--首先我们创建用于文件操作的目录
SQL> create directory colin_dir as '/opt/oracle/oradata/orcl/colin';
Directory created
PS:/opt/oracle/oradata/orcl/colin这个目录必须在服务器上创建
--第二步授予读写权限
SQL> grant read,write on directory colin_dir to dba;
Grant succeeded
偶是直接把这个目录的读写权限授给了DBA这个角色,因为我当前的用户就是DBA,也可以通过sys用户直接授予当前用户
[oracle@localhost colin]$ sqlplus /nolog
SQL*Plus: Release 10.2.0.3.0 - Production on Thu Oct 21 17:34:36 2010
Copyright (c) 1982, 2006, Oracle. All Rights Reserved.
SQL> conn /as sysdba
Connected.
SQL> grant read,write on directory COLIN_DIR to gamedata_fs3;
Grant succeeded.
--第三步创建一个简单的写数据到文件excel的过程
create or replace procedure test_uti_file(test_file in varchar2) is
v_content char(4);
cursor test_ufile_cur is
select id || chr(9) || name || chr(9) from uti_file_test;
l_file utl_file.file_type;
begin
l_file := utl_file.fopen('COLIN_DIR', test_file, 'w');
open test_ufile_cur;
loop
fetch test_ufile_cur
into v_content;
exit when test_ufile_cur %notfound;
utl_file.put_line(l_file, v_content, false);
end loop;
close test_ufile_cur;
utl_file.fflush(l_file);
utl_file.fclose(l_file);
end;
PS:上面的COLIN_DIR的目录名必须大写,test_file文件不必事先建立
--第四步测试过程
begin
-- Call the procedure
test_uti_file(test_uti_file.xls);
end;
--最后可以到服务器查看生成的test_uti_file.xls文件了
1 a
2 b
|