oracle9i和10G中对于utl_file包的使用是有些区别的。
在9i中类似于utl_file.fopen('E:/work', 'test.txt', 'w');这种直接指定路径的写法是可以输出到文件的。
但是在10g中这种写法就会报错。
因此如果对于动态输出到不同目录的要求来说10g就没有办法直接实现了。
开发9i和10g通用的plsql
只能是在外部根据情况用SQL设定不同的目录
create or replace directory exp_dir as '/tmp'
然后在plsql内部根据情况调用
utl_file.fopen('EXP_DIR', 'test.txt', 'w');
注意这里一定要大写'EXP_DIR'不然可能有问题。
备注
9i中指定目录的方法
1。alter system set utl_file_dir='e:/utl' scope=spfile
2。在init.ora文件中设定UTL_FILE=E:/utl或者UTL_FILE_DIR=E:/utl
3。使用上面10g中推荐的方法
目录创建以后,就可以把读写权限授予特定用户
GRANT READ[,WRITE] ON DIRECTORY directory TO username;
执行权限授予特定用户
GRANT EXECUTE ON utl_file TO username;
可以查询dba_directories查看所有directory
select * from dba_directories;
可以使用drop directory删除这些路径
drop directory exp_dir;
例子
SQL> declare
2 fhandle utl_file.file_type;
3 begin
4 fhandle := utl_file.fopen('UTL_FILE_DIR', 'example.txt', 'w');
5 utl_file.put_line(fhandle , 'eygle test write one');
6 utl_file.put_line(fhandle , 'eygle test write two');
7 utl_file.fclose(fhandle);
8 end;
9 /
参考文章
http://leeecho.spaces.live.com/blog/cns!F4D3BD04417581A3!328.entry