1 utl_file_dir参数定义 utl_file_dir是oracle中的一个“静态参数”,可以设置一个或多个路径。用于在pl/sql中进行文件i/o操作(
1 utl_file_dir参数定义
utl_file_dir是oracle中的一个“静态参数”,,可以设置一个或多个路径。用于在pl/sql中进行文件i/o操作(可以用utl_file包)时指定路径。utl_file_dir是oracle中的一个“静态参数”,可以设置一个或多个路径。用于在pl/sql中进行文件i/o操作(可以用utl_file包)时限定路径,utl_file包只能在指定路径下创建,读取文件。utl_file_dir为空时,则不限定路径。
2 utl_file包简介
在pl/sql中没有直接的文件i/o接口,一般在调试程序时可以使用oracle自带的dbms_output包的put_line函数(即向屏幕进行i/o 操作),但是不能对磁盘文件进行i/o操作。文件i/o对于数据库的开发来说显得很重要,比如如果数据库中的一部分数据来自于磁盘文件,那么就需要使用i/o接口把数据导入到数据库中来。
3 实验
3.1 设置utl_file_dir参数
sql> alter system set utl_file_dir='/u01/app/oracle' scope=spfile;
system altered.
sql> startup force;
sql> show parameter utl_file
name type value
-------------------------------- ----------- ------------------------------
utl_file_dir string /u01/app/oracle
设置多个路径:
sql> alter system set utl_file_dir='/u01/app/oracle', '/oradata' scope=spfile;
system altered.
sql> startup force
name type value
-------------------------------- ----------- ------------------------------
utl_file_dir string /u01/app/oracle, /oradata
3.2 utl_file的io操作
sql> declare
fn utl_file.file_type;
begin
fn := utl_file.fopen('/u01/app/oracle', 'utl_test.txt', 'w');
utl_file.fclose(fn);
end;
/
pl/sql procedure successfully completed.
不是utl_file_dir所指定的路径时,使用fopen方法时就会报错:
sql> declare
fn utl_file.file_type;
begin
fn := utl_file.fopen('/u01/app/oracle/admin', 'utl_test.txt', 'w');
utl_file.fclose(fn);
end;
/
declare
*
error at line 1:
ora-29280: invalid directory path
ora-06512: at sys.utl_file, line 33
ora-06512: at sys.utl_file, line 436
ora-06512: at line 4
为了避免上面的错误,可以使用路径对象。
sql> create directory dir_test as '/oradata';
directory created.
sql> declare
fn utl_file.file_type;
begin
fn := utl_file.fopen('dir_test', 'test.txt', 'w');
utl_file.fclose(fn);
end;