create directory让我们可以在oracle数据库中灵活的对文件进行读写操作,极大的提高了oracle的易用性和可扩展性。
其语法为:
create [or replace] directory directory as 'pathname';
本案例具体创建如下:
create or replace directory exp_dir as '/tmp';
|
目录创建以后,就可以把读写权限授予特定用户,具体语法如下:
grant read[,write] on directory directory to username;
例如:
grant read, write on directory exp_dir to eygle;
|
此时用户eygle就拥有了对该目录的读写权限。
让我们看一个简单的测试:
sql> create or replace directory utl_file_dir as '/opt/oracle/utl_file';
directory created.
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 /
pl/sql procedure successfully completed.
sql> !
[oracle@jumper 9.2.0]$ more /opt/oracle/utl_file/example.txt
eygle test write one
eygle test write two
[oracle@jumper 9.2.0]$
|
类似的我们可以通过utl_file来读取文件:
sql> declare
2 fhandle utl_file.file_type;
3 fp_buffer varchar2(4000);
4 begin
5 fhandle := utl_file.fopen ('utl_file_dir','example.txt', 'r');
6
7 utl_file.get_line (fhandle , fp_buffer );
8 dbms_output.put_line(fp_buffer );
9 utl_file.get_line (fhandle , fp_buffer );
10 dbms_output.put_line(fp_buffer );
11 utl_file.fclose(fhandle);
12 end;
13 /
eygle test write one
eygle test write two
pl/sql procedure successfully completed.
|
可以查询dba_directories查看所有directory.
sql> select * from dba_directories;
owner directory_name directory_path
------------------------------ ------------------------------ ------------------------------
sys utl_file_dir /opt/oracle/utl_file
sys bdump_dir /opt/oracle/admin/conner/bdump
sys exp_dir /opt/oracle/utl_file
|
可以使用drop directory删除这些路径.
sql> drop directory exp_dir;
directory dropped
sql> select * from dba_directories;
owner directory_name directory_path
------------------------------ ------------------------------ ------------------------------
sys utl_file_dir /opt/oracle/utl_file
sys bdump_dir /opt/oracle/admin/conner/bdump
|