Oracle存儲(chǔ)過(guò)程讀寫(xiě)文件
Oracle存儲(chǔ)過(guò)程讀寫(xiě)文件是實(shí)現(xiàn)文件與數(shù)據(jù)庫(kù)表之間交互的重要手段,下面就為您詳細(xì)介紹Oracle存儲(chǔ)過(guò)程讀寫(xiě)文件方面的知識(shí),希望對(duì)您能有所幫助。
有時(shí)候我們需要在文件與數(shù)據(jù)庫(kù)表之間利用程序來(lái)實(shí)現(xiàn)兩者的交互,這里可以利用UTL_FILE包實(shí)現(xiàn)對(duì)文件的I/O操作.下面就分別介紹文件寫(xiě)表以及表數(shù)據(jù)寫(xiě)文件.
[1]表信息導(dǎo)出到文件
在SUSE上建議一個(gè)文件夾/home/zxin10/file,然后對(duì)其chmod g+w file進(jìn)行授權(quán)(否則無(wú)法導(dǎo)出到文件),再對(duì)您指定的路徑(/home/zxin10/file)向Oracle的系統(tǒng)表sys.dir$進(jìn)行注冊(cè)(否則也是無(wú)法成功將信息導(dǎo)出到文件),操作完后可以查詢(xún)sys.dir$可以看到表中的OS_PATH中有您指定的路徑位置.
注冊(cè)方式:執(zhí)行SQL語(yǔ)句create or replace directory BBB as '/home/zxin10/file'; 即可
存儲(chǔ)過(guò)程如下:(寫(xiě)文件時(shí),文件名可以不用先創(chuàng)建,程序中會(huì)自動(dòng)創(chuàng)建指定文件)
- CREATE OR REPLACE PROCEDURE V3_SUB_FETCH_TEST_2
- (
- V_TEMP VARCHAR2,
- --1為成功,0為失敗
- v_retvalue OUT NUMBER
- )
- AS
- --游標(biāo)定義
- type ref_cursor_type is REF CURSOR;
- cursor_select ref_cursor_type;
- select_cname varchar2(1000);
- v_file_handle utl_file.file_type;
- v_sql varchar2(1000);
- v_filepath Varchar2(500);
- v_filename Varchar2(500);
- --緩沖區(qū)
- v_results Varchar2(500);
- v_pid varchar2(1000);
- v_cpcnshortname Varchar2(500);
- begin
- v_filepath := V_TEMP;
- if v_filepath is null then
- v_filepath := '/home/zxin10/file3';
- end if;
- v_filename:='free_'|| substr(to_char(sysdate,'YYYYMMDDHH24MI'),1,10) ||'.all' ;
- --游標(biāo)開(kāi)始
- select_cname:='select cpid,cpcnshortname from zxdbm_ismp.scp_basic';
- --打開(kāi)一個(gè)文件句柄 ,同時(shí)fopen的***個(gè)參數(shù)必須是大寫(xiě)
- v_file_handle:=utl_file.fopen('BBB',v_filename,'A');
- Open cursor_select For select_cname;
- Fetch cursor_select into v_pid,v_cpcnshortname;
- While cursor_select%Found
- Loop
- v_results := v_pid||'|'||v_cpcnshortname;
- --將v_results寫(xiě)入文件
- utl_file.put_line(v_file_handle,v_results);
- Fetch cursor_select into v_pid,v_cpcnshortname;
- End Loop;
- Close cursor_select;--關(guān)閉游標(biāo)
- utl_file.fClose(v_file_handle);--關(guān)閉句柄
- v_retvalue :=1;
- exception when others then
- v_retvalue :=0;
- end V3_SUB_FETCH_TEST_2;
[2]將文件信息導(dǎo)入到表中
和上面一樣,先對(duì)指定文件路徑進(jìn)行chmod,然后想Oracle的sys.dir$進(jìn)行路徑注冊(cè).
文件zte.apsuic位于/home/zxin10/file下,其數(shù)據(jù)格式:
1|22|cheng
2|33|zhou
3|44|heng
4|55|yaya
表LOADDATA腳本:
- -- Create table
- create table LOADDATA
- (
- ID VARCHAR2(50),
- AGE VARCHAR2(50),
- NAME VARCHAR2(50)
- )
- /
程序如下:(讀取文件時(shí),指定文件名一定要預(yù)先存在,否則程序會(huì)失敗)
- create or replace directory BBB as '/home/zxin10/file';
- /
- --作用法是將特定的文件路徑信息想Oracle注冊(cè)(注冊(cè)信息存放在sys.dir$表中)
- CREATE OR REPLACE PROCEDURE V3_SUB_FETCH_TEST_3
- (
- --文件中的信息導(dǎo)入表中
- V_TEMP VARCHAR2,
- v_retvalue OUT NUMBER --1 成功 ,0失敗
- AS
- v_file_handle utl_file.file_type;
- v_sql varchar2(1000);
- v_filepath Varchar2(500);
- v_filename Varchar2(500);
- --文件到表字段的映射
- v_id varchar2(1000);
- v_age varchar2(1000);
- v_name varchar2(1000);
- --緩沖區(qū)
- v_str varchar2(1000);
- --列指針
- v_i number;
- --字符串定位解析指針
- v_sposition1 number;
- v_sposition2 number;
- begin
- v_filepath := V_TEMP;
- if v_filepath is null then
- v_filepath := '/home/zxin10/file';
- end if;
- v_filename:='zte.apsuic';
- --v_sql:= 'create or replace directory CCC as '''|| v_filepath || '''';
- --execute immediate v_sql;
- v_file_handle:=utl_file.fopen('CCC',v_filename,'r');
- Loop
- --將文件信息讀取到緩沖區(qū)v_str中,每次讀取一行
- utl_file.get_line(v_file_handle,v_str);
- --dbms_output.put_line(v_str);
- --針對(duì)每行的列數(shù)
- v_i := 1;
- --針對(duì)字符串每次的移動(dòng)指針
- v_sposition1 := 1;
- --文件中每行信息3列,循環(huán)3次
- FOR I IN 1..3 loop
- --當(dāng)instr(v_str, '|', 6)其中v_str為1|22|wuzhuocheng ,它返回0
- v_sposition2 := instr(v_str, '|', v_sposition1);
- --字符串解析正常情況
- if v_sposition2 <> 0 then
- if v_i=1 then
- v_id := substr(v_str, v_sposition1, v_sposition2 - v_sposition1); --***列
- elsif v_i=2 then
- v_age := substr(v_str, v_sposition1, v_sposition2 - v_sposition1); --第二列
- elsif v_i=3 then
- v_name := substr(v_str, v_sposition1, v_sposition2 - v_sposition1); --第三列
- else
- return;
- end if;
- --字符串解析異常情況
- else
- if v_i=1 then
- v_id := substr(v_str, v_sposition1); --***列
- elsif v_i=2 then
- v_age := substr(v_str, v_sposition1); --第二列
- elsif v_i=3 then
- v_name := substr(v_str, v_sposition1); --第三列
- else
- return;
- end if;
- end if;
- v_sposition1 := v_sposition2 + 1;
- v_i := v_i+1;
- end loop;
- --每列循環(huán)完后將信息insert into表中
- insert into zxdbm_ismp.loaddata values(v_id,v_age,v_name);
- End Loop;
- --關(guān)閉句柄
- utl_file.fClose(v_file_handle);
- v_retvalue :=1;
- exception when others then
- v_retvalue :=0;
- end V3_SUB_FETCH_TEST_3;
【編輯推薦】
Oracle讀寫(xiě)文件bfilename的實(shí)例
Oracle XE自帶數(shù)據(jù)庫(kù)創(chuàng)建的過(guò)程