根据Oracle实例编写一个使用UTL_FILE读取TXT档,然后写数据库表的存储过程:
create or replace procedure loadfiledata(p_filename varchar2) as
v_filehandle utl_file.file_type;
v_text varchar2(150);
v_name test_loadfile.name%type;
v_addr_jd test_loadfile.addr_jd%type;
v_firstlocation number;
v_secondlocation number;
v_totalinserted number;
begin
if (p_filename is null) then
return;
end if ;
v_totalinserted:=0;
v_filehandle:=utl_file.fopen('RECV_AREA',p_filename,'R');
loop
begin
utl_file.get_line(v_filehandle,v_text);
exception
when no_data_found then
exit;
end;
v_firstlocation :=instr(v_text,',',1,1);
v_secondlocation:=instr(v_text,',',1,2);
v_name :=substr(v_text,1,v_firstlocation-1);
v_addr_jd :=substr(v_text,v_firstlocation+1);
insert into pms_item(item_id,item_name) values (v_name,v_addr_jd);
commit;
end loop;
end loadfiledata;
测试执行>1900万数据大概需要一个小时(笔记本环境):
SQL> exec loadfiledata('pmsreport.csv');
PL/SQL procedure successfully completed
Executed in 3903.503 seconds
导入数据建议使用load data 速度比此快一半多。
create or replace procedure loadfiledata(p_filename varchar2) as
v_filehandle utl_file.file_type;
v_text varchar2(150);
v_name test_loadfile.name%type;
v_addr_jd test_loadfile.addr_jd%type;
v_firstlocation number;
v_secondlocation number;
v_totalinserted number;
begin
if (p_filename is null) then
return;
end if ;
v_totalinserted:=0;
v_filehandle:=utl_file.fopen('RECV_AREA',p_filename,'R');
loop
begin
utl_file.get_line(v_filehandle,v_text);
exception
when no_data_found then
exit;
end;
v_firstlocation :=instr(v_text,',',1,1);
v_secondlocation:=instr(v_text,',',1,2);
v_name :=substr(v_text,1,v_firstlocation-1);
v_addr_jd :=substr(v_text,v_firstlocation+1);
insert into pms_item(item_id,item_name) values (v_name,v_addr_jd);
commit;
end loop;
end loadfiledata;
测试执行>1900万数据大概需要一个小时(笔记本环境):
SQL> exec loadfiledata('pmsreport.csv');
PL/SQL procedure successfully completed
Executed in 3903.503 seconds
导入数据建议使用load data 速度比此快一半多。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16381228/viewspace-759158/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/16381228/viewspace-759158/