oracle利用使用utl_file包
create or replace procedure loadfiledata(p_path varchar2,p_filename varchar2) is
v_filehandle utl_file.file_type; --定義一個文件句柄
v_text varchar2(100); --存放文本
v_name test.name%type;
v_id test.autoid%type;
v_firstlocation number;
v_secondlocation number;
v_totalinserted number;
begin
if (p_path is null or p_filename is null) then
goto to_end;
end if;
v_totalinserted:=0;
/*open specified file*/
v_filehandle:=utl_file.fopen(p_path,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_id:=substr(v_text,1,v_firstlocation-1);
v_name:=substr(v_text,v_firstlocation+1);
/*插入數據庫操作*/
insert into test
values (v_id,v_name);
commit;
end loop;
<;>;
null;
end loadfiledata;
================我創建表=====
3. 測試環境
首先要創建一個目標表,它用來存儲文件中的數據:
CREATE TABLE TEST (
autoid varchar2(10);
name varchar2(20));
==========================
declare
v_path varchar2(200);
v_filename varchar2(200);
begin
v_path:='F: ';
v_filename:='地址信息.txt';
loadfiledata(v_path,v_filename);
end;
/
/* 由於Oracle數據庫對包創建的目錄有一個安全管理的問題,所以並不是所有的文件目錄能夠被UTL_FILE包所訪問,
要更新這種目錄設置,就得到init.ora裡將UTL_FILE_DIR域設置為*,這樣UTL_FILE包就可以對所有的目錄文件進行訪問了,UTL_FILE_DIR我設置有正確*/
=====出現錯誤呀提示!======
ORA-06510: PL/SQL: 無法處理的用戶自定義異常事件
ORA-06512: 在"SYS.UTL_FILE", line 98
ORA-06512: 在"SYS.UTL_FILE", line 157
ORA-06512: 在"SYSTEM.LOADFILEDATA2", line 15
ORA-06512: 在line 1
==================
創建一個目標表,它用來存儲文件中的數據,求救呀!不行呀!大家幫幫忙呀!若是下面的例子,把數據寫到文本文件中,可以呀!
===================
declare
file_handle utl_file.file_type;
begin
file_handle:=utl_file.fopen('c:temp','sss.txt','A');
utl_file.put_line(file_handle,'寫入的信息');
utl_file.fclose(file_handle);
END;
========
在init.ora中加入UTL_FILE_DIR = C:TEMP
重起後就行了[@more@]
create or replace procedure loadfiledata(p_path varchar2,p_filename varchar2) is
v_filehandle utl_file.file_type; --定義一個文件句柄
v_text varchar2(100); --存放文本
v_name test.name%type;
v_id test.autoid%type;
v_firstlocation number;
v_secondlocation number;
v_totalinserted number;
begin
if (p_path is null or p_filename is null) then
goto to_end;
end if;
v_totalinserted:=0;
/*open specified file*/
v_filehandle:=utl_file.fopen(p_path,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_id:=substr(v_text,1,v_firstlocation-1);
v_name:=substr(v_text,v_firstlocation+1);
/*插入數據庫操作*/
insert into test
values (v_id,v_name);
commit;
end loop;
<;>;
null;
end loadfiledata;
================我創建表=====
3. 測試環境
首先要創建一個目標表,它用來存儲文件中的數據:
CREATE TABLE TEST (
autoid varchar2(10);
name varchar2(20));
==========================
declare
v_path varchar2(200);
v_filename varchar2(200);
begin
v_path:='F: ';
v_filename:='地址信息.txt';
loadfiledata(v_path,v_filename);
end;
/
/* 由於Oracle數據庫對包創建的目錄有一個安全管理的問題,所以並不是所有的文件目錄能夠被UTL_FILE包所訪問,
要更新這種目錄設置,就得到init.ora裡將UTL_FILE_DIR域設置為*,這樣UTL_FILE包就可以對所有的目錄文件進行訪問了,UTL_FILE_DIR我設置有正確*/
=====出現錯誤呀提示!======
ORA-06510: PL/SQL: 無法處理的用戶自定義異常事件
ORA-06512: 在"SYS.UTL_FILE", line 98
ORA-06512: 在"SYS.UTL_FILE", line 157
ORA-06512: 在"SYSTEM.LOADFILEDATA2", line 15
ORA-06512: 在line 1
==================
創建一個目標表,它用來存儲文件中的數據,求救呀!不行呀!大家幫幫忙呀!若是下面的例子,把數據寫到文本文件中,可以呀!
===================
declare
file_handle utl_file.file_type;
begin
file_handle:=utl_file.fopen('c:temp','sss.txt','A');
utl_file.put_line(file_handle,'寫入的信息');
utl_file.fclose(file_handle);
END;
========
在init.ora中加入UTL_FILE_DIR = C:TEMP
重起後就行了[@more@]
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/101162/viewspace-1009209/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/101162/viewspace-1009209/