« Jonathan Lewis: Can we have a sensible debate ? |
Blog首页
| Solaris Open File Limit »
Using Create directory & UTL_FILE in Oracle
作者:
eygle |【转载时请以超链接形式标明文章
出处和作者信息及
本声明】
链接:
http://www.eygle.com/archives/2005/04/using_create_di.html
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
|
历史上的今天...
>>
2006-04-22文章:
------
这篇 【
Using Create directory & UTL_FILE in Oracle】来自
www.eygle.com |
CSDN技术网摘|
del.icio.us|
365Key
By eygle on 2005-04-22 11:57 |
Comments (4) |
Posted to
SQL.PLSQL | Edit |Pageviews:
搜索本站:
建议把您作为语法的
create or replace directory exp_dir as '/tmp';
grant read, write on directory exp_dir to eygle;
这两句用标准语法格式给出吧,或许我比较愚钝,但的确有些感觉不清晰。建议换成:
CREATE [OR REPLACE] DIRECTORY directory AS 'pathname';
GRANT READ[,WRITE] ON DIRECTORY directory TO username;
此外,您在给出的测试中,并没有对 UTL_FILE_DIR 进行授权,当然您当时测试肯定没有问题,但作为HowTo,最好完整示例您给出的语法。
浅见,请包涵!
Posted by: dba_mx at December 28, 2005 8:40 AM
Posted by: eygle at October 18, 2006 9:40 AM
是不是只有用sys用户创建目录,在把权限授予其他用户?
Posted by: hanjs at January 27, 2007 4:07 PM
I had a lot of time finding a soluction
So good you note
Thanks you
Best regards
Antonio
Posted by: Antonio May at February 8, 2007 5:48 AM
发表留言: