utl_file实现oracle与OS的I/O交互

此文从某大神copy。

1、    建目录,并给相应用户授目录读写权限和UTL_FILE包执行权限

-- Create directory create or replace directory UTL_DIR as 'D:\AIX\ora_log\'; --以sys as sysdba grant read, write on directory SYS.UTL_DIR to JBK with grant option; grant execute on SYS.UTL_FILE to JBK;


 

  

2、    拷贝OS文件

 

BEGIN utl_file.fcopy('UTL_DIR', 'agmt_bal.dat', 'UTL_DIR', 'agmt_bal_'||to_char(sysdate-1,'YYYYMMDD')||'.dat'); END;

 

 

3、    读取OS文件

set serveroutput on ; declare f utl_file.file_type; x varchar2(2000); begin f := utl_file.fopen('UTL_DIR','edw_t01_repay_detail.txt','r'); loop begin utl_file.get_line(f,x); dbms_output.put_line(x); Exception when no_data_found then exit; end; end loop; end;


 

 

 

4、    OS文件中写ORACLE日志

CREATE OR REPLACE PROCEDURE write_log_to_file (write_text varchar2) IS fileID UTL_FILE.FILE_TYPE; BEGIN fileID := UTL_FILE.FOPEN('UTL_DIR', 'gbicc_'||to_char(sysdate,'YYYYMMDD')||'.log', 'W'); UTL_FILE.PUT_LINE(fileID,write_text||' log is ok!'); UTL_FILE.FCLOSE(fileID); END;

 

 

5、    oracle表导出到OS文件

 

(1)、简易代码 declare num number; j number := 0; sname varchar2(200); numfile UTL_FILE.FILE_TYPE; temp varchar2(1000); cursor curemp is select * from edw.edw_t01_repay_detail; begin numfile := utl_file.fopen('UTL_DIR', 'edw_t01_repay_detail.txt', 'w'); for i in curemp loop j := j + 1; temp := j || ':' || i.agmt_no || '|' || i.agmt_sub_no || '|' || i.repay_date || '|' || i.core_due_bill_no; utl_file.put_line(numfile, temp); --utl_file.new_line(numfile); end loop; utl_file.fclose(numfile); end; (2)、存储过程实现 CREATE OR REPLACE PROCEDURE write_data_to_file IS fileID UTL_FILE.FILE_TYPE; BEGIN fileID := UTL_FILE.FOPEN('UTL_DIR', 'agmt_bal.dat', 'W'); FOR agmt_bal IN (select * from jbk.edw_t01_agmt_bal_h) LOOP UTL_FILE.PUT_LINE(fileID,agmt_bal.agmt_no||'|'||agmt_bal.agmt_sub_no||'|'||agmt_bal.agmt_bal||'|'||to_char(agmt_bal.start_date,'YYYY-MM-DD')); END LOOP; UTL_FILE.FCLOSE(fileID); END;
 

 

6、    有较全信息的可外部调度的向OS中写日志的过程

create or replace procedure write_txtfile( -- 写一个字符串到指定文本文件中 path in varchar2, name in varchar2, pstr in varchar2 ) as l_output utl_file.file_type; str varchar2(1000); begin l_output:=utl_file.fopen(path,name,'a',2000); --每行最大字节数最多为32K bytes --l_output:=utl_file.fopen(path,name,'a'); -- 每行最大字节数最多为1023 bytes utl_file.put_line(l_output,pstr); utl_file.fclose(l_output); exception when utl_file.invalid_path then raise_application_error(-20001,'INVALID_PATH!'); when utl_file.invalid_mode then raise_application_error(-20002,'INVALID_MODE!'); when utl_file.invalid_filehandle then raise_application_error(-20003,'INVALID_FILEHANDLE!'); when utl_file.invalid_operation then raise_application_error(-20004,'INVALID_OPERATION!'); when utl_file.read_error then raise_application_error(-20005,'READ_ERROR!'); when utl_file.write_error then raise_application_error(-20006,'WRITE_ERROR!'); when utl_file.internal_error then raise_application_error(-20007,'INTERNAL_ERROR!'); when others then str:=sqlerrm(sqlcode); dbms_output.put_line(str); end;

 

 

 

 

 

 

 

你可能感兴趣的:(utl_file,oracle与os)