Oracle提供的文件操作包UTL_FILE包中的UTL_FILE.FOPEN负责打开一个文件。 UTL_FILE.FOPEN(location in varchar2, filename in varchar2, open_mode in varchar2) return FILE_TYPE; Location 是路径参数, FILENAME 是文件名, OPEN_MODE是打开模式,‘R‘是读文本,‘W‘是写文本,‘A‘是附加文本,参数不分大小写,如果指定‘A‘但是文件不存在,它会用‘W‘先创建出来,‘W‘有覆盖的功能; 其中的location并不能简单的指定为‘D:\temp‘等路径,要建立一个DIRECTORY变量并付给权限(必须以DBA身份登录): Sql代码 create or replace directory D_OUTPUT as ‘D:\TEMP‘; grant read,write on directory D_OUTPUT to testdb; GRANT EXECUTE ON utl_file TO testdb; 之后就可以用UTL_FILE包建立文件了 Sql代码 V_FILE UTL_FILE.FILE_TYPE; V_FILE := UTL_FILE.FOPEN(‘D_OUTPUT‘, ‘Data.txt‘, ‘w‘); 就可以在数据库服务器的D:\TEMP建立Data.txt 源文档 <http://lveyo.javaeye.com/blog/351416> create or replace procedure PRO_TO_TXT(FILE_OWNER VARCHAR2,FILE_NAME VARCHAR2) is file_handle utl_file.file_type; STOR_TEXT VARCHAR2(4000); N NUMBER; I NUMBER; begin I:=1; SELECT MAX(LINE) INTO N FROM ALL_SOURCE WHERE OWNER=‘‘ || FILE_OWNER || ‘‘ AND NAME=‘‘ || FILE_NAME || ‘‘ AND TYPE= ‘PROCEDURE‘; file_handle:=utl_file.fopen( ‘/oradata/procedure_bak‘, ‘zhfx117.txt‘, ‘A‘); utl_file.put_line(file_handle, ‘PROCEDURE NAME: ‘||FILE_NAME); WHILE I <=N LOOP SELECT TEXT INTO STOR_TEXT FROM ALL_SOURCE WHERE OWNER=FILE_OWNER AND NAME=FILE_NAME AND TYPE= ‘PROCEDURE‘ AND LINE= I; I:=I+1; utl_file.put_line(file_handle,stor_text); END LOOP; utl_file.fclose(file_handle); commit; end PRO_TO_TXT;
时间: 2024-10-26 23:43:01