如何使用文本文件中的数据填充列,其中每行是不同的列?文本文件如下所示:
Amsterdam Aalsmeerweg
Aalsmeerweg
14
Amsterdam
NL
1059NJ
0204122131
Amsterdam Bos en Lommerweg
Bos en Lommerweg
215
AMSTERDAM
NL
1055DT
0206847676
Amsterdam Ceintuurbaan
Ceintuurbaan
314
AMSTERDAM
NL
1072GL
0204705292
This post没有帮助我。
我的代码如下所示:
set serveroutput on;
CREATE or replace DIRECTORY USER_DIR AS '/home/renejanssen/';
GRANT READ ON DIRECTORY USER_DIR TO PUBLIC;
declare
V1 VARCHAR2(200);
F1 UTL_FILE.FILE_TYPE;
f_name WINKEL.NAAM%type;
f_adres varchar2(255);
f_homenr WINKEL.HUISNR%TYPE;
f_city varchar2(100);
f_countrcode varchar(255);
f_zipcode WINKEL.POSTCODE%TYPE;
f_phonenr WINKEL.TELNR%TYPE;
v_counter number DEFAULT 1; --counter for ID
BEGIN
F1 := UTL_FILE.FOPEN('USER_DIR','test','R');
Loop
BEGIN
UTL_FILE.GET_LINE(F1,V1);
dbms_output.put_line(v_counter || ' = ' || V1);
EXCEPTION WHEN No_Data_Found THEN EXIT;
dbms_output.put_line('niets');
END;
f_name := substr(V1,1);
f_adres := substr(V1,1);
f_homenr := substr(V1,1);
f_countrcode := substr(V1,1);
f_zipcode := substr(V1,1);
insert INTO WINKEL (ID, NAAM, HUISNR, POSTCODE, TELNR )
values(v_counter, f_name, f_homenr, f_zipcode, f_phonenr);
v_counter := v_counter +1;
end loop;
IF UTL_FILE.IS_OPEN(F1) THEN
dbms_output.put_line('File is Open');
end if;
UTL_FILE.FCLOSE(F1);
END;
答案 0 :(得分:1)
如果文件中的每个条目总是有七行显示,后面跟一个空白行,你可以直接在PL / SQL变量中进行七次读取:
declare
v1 varchar2(200);
f1 utl_file.file_type;
f_name winkel.naam%type;
f_adres varchar2(255);
f_homenr winkel.huisnr%type;
f_city varchar2(100);
f_countrcode varchar(255);
f_zipcode winkel.postcode%type;
f_phonenr winkel.telnr%type;
v_counter number default 1; --counter for ID
begin
f1 := utl_file.fopen('D42','test','R');
loop
begin
utl_file.get_line(f1, f_name);
utl_file.get_line(f1, f_adres);
utl_file.get_line(f1, f_homenr);
utl_file.get_line(f1, f_city);
utl_file.get_line(f1, f_countrcode);
utl_file.get_line(f1, f_zipcode);
utl_file.get_line(f1, f_phonenr);
exception
when no_data_found then
exit;
dbms_output.put_line('niets');
end;
insert into winkel(id, naam, huisnr, postcode, telnr)
values (v_counter, f_name, f_homenr, f_zipcode, f_phonenr);
v_counter := v_counter +1;
begin
-- consume possible blank line; don't error as final entry may not have one
utl_file.get_line(f1, f_adres);
dbms_output.put_line('extra line: ' || f_adres);
exception
when no_data_found then
dbms_output.put_line('extra line: ' || sqlerrm);
null;
end;
end loop;
if utl_file.is_open(f1) then
dbms_output.put_line('File is Open');
end if;
utl_file.fclose(f1);
end;
/
使用您的数据文件填充表格:
从winkel中选择*;
ID NAAM HUISNR POSTCODE TELNR
---------- ---------------------------------------- ---------- ---------- --------------
1 Amsterdam Aalsmeerweg 14 1059NJ 0204122131
2 Amsterdam Bos en Lommerweg 215 1055DT 0206847676
3 Amsterdam Ceintuurbaan 314 1072GL 0204705292