Author: eygle [Copyright Disclaimer: When reprinting, please be sure to mark the original source and author information of the article in the form of hyperlinks and this statement]
Link: http://www.eygle.com/archives/2005/04/using_create_di.html
Create directory allows us to flexibly read and write files in Oracle databases, greatly improving the ease of use and scalability of oracle.
Its syntax is:
Create [or replace] DirectoryDirectoryAs'Pathname';
This case is created as follows:
create or replace directory exp_dir as '/tmp';
|
After the directory is created, you can grant the read and write permissions to specific users. The specific syntax is as follows:
Grant read [, write] On directoryDirectoryToUsername;
For example:
grant read, write on directory exp_dir to eygle;
|
In this case, the user eygle has the permission to read and write the directory.
Let's look at a simple test:
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]$
|
Similarly, you can use utl_file to read files:
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.
|
You can query dba_directories to view all directories.
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
|
You can use drop directory to delete these paths.
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
|