Thursday, 23 June 2011

Oracle UTL Files

UTL_FILE
The Oracle supplied package UTL_FILE can be used to read and write files that are located on the server. It cannot be used to access files locally, that is on the computer where the client is running.

Procedures/Functions
fopen
function fopen returns record
(
location in varchar2 ,
filename in varchar2 ,
open_mode in varchar2 ,
max_linesize in binary_integer default
);
Opens a file. Takes four parameters: location, filename, open_mode and max_linesize
location: must be either an (existing) directory on the server AND be in the utl_file_dir paramter, or
a directory.
open_mode: 'W' for writing access or 'R' for reading access.

get_line
procedure get_line (
file in record ,
buffer out varchar2 ,
len in binary_integer default
);
Reads a line from the opened file.


put_line
procedure put_line (
file in record ,
buffer in varchar2 ,
autoflush in boolean default
);
Writes a line into the opened file.
If a line was already written, it starts the line with CR/LF. This implies that the file, when being written into, does not end with CR/LF.

Example
The following two procedures show how to use utl_file to write to and read from a file using PL/SQL. It doesn't do very much let alone something useful, but it can be extended.
In order to use it, make sure the utl_file_dir paramter is set:
select value from v$parameter where name = 'utl_file_dir';
The value returned is actually the path that you must use in the arguments to utl_file_test_read and utl_file_test_write.
utl_file_test_write writes two lines into the file specified with the parameters path and filename.

No comments:

Post a Comment