IO in PLSQL
IO in PLSQL
I/O in PL/SQL:
Managing Files
Displaying Output to Screen
Sending Email
Steven Feuerstein
PL/SQL Evangelist, Quest Software
steven.feuerstein@quest.com
PL/SQL Obsession - www.ToadWorld.com/SF
$ORACLE_HOME/Rdbms/Admin
create_directories.sql
Copyright 2000-2007 Steven Feuerstein - Page 11 utlfile_92.sql
Opening a File
DECLARE
fid UTL_FILE.FILE_TYPE;
BEGIN
fid := UTL_FILE.FOPEN ('TEMP_DIR', 'test.txt', 'W',
max_linesize => 32767);
UTL_FILE.fclose (fid);
END;
DECLARE
fid UTL_FILE.FILE_TYPE;
BEGIN
fid := UTL_FILE.FOPEN ('TEMP', 'test.txt', 'R');
UTL_FILE.GET_LINE (fid, myline);
UTL_FILE.FCLOSE (fid);
END;
Can only read from a file opened with the "R" mode.
Can only read one line at a time.
The NO_DATA_FOUND exception is raised if you read
past the end of the file.
That's more than a little bit silly and a bad idea.
read_from_file.sql
infile.sf
Copyright 2000-2007 Steven Feuerstein - Page 14 eqfiles.sf
A better GET_LINE function
create_file.sp
write_to_file.sql
read_and_write.sql
display_file.sql
Copyright 2000-2007 Steven Feuerstein - Page 16 genaa.sql
Flushing content to file
DECLARE
file_suffix VARCHAR2 (100)
:= TO_CHAR (SYSDATE, 'YYYYMMDDHH24MISS');
BEGIN
UTL_FILE.fcopy (
src_location => 'DEVELOPMENT_DIR',
src_filename => 'archive.zip',
dest_location => 'ARCHIVE_DIR',
dest_filename => 'archive' || file_suffix || '.zip'
);
END;
BEGIN
UTL_FILE.fremove (
src_location => 'DEVELOPMENT_DIR',
src_filename => 'archive.zip'
);
EXCEPTION
/* Don't forget to check for errors! */
WHEN UTL_FILE.delete_failed
THEN
... Deal with failure to remove
END;
fremove.sql
fileIO92.pkg
DECLARE
file_suffix VARCHAR2 (100) := TO_CHAR (SYSDATE, 'YYYYMMDD');
BEGIN
-- Rename/move the entire file in a single step.
UTL_FILE.frename (
src_location => 'DEVELOPMENT_DIR',
src_filename => 'archive.zip',
dest_location => 'ARCHIVE_DIR',
dest_filename => 'archive' || file_suffix || '.zip',
overwrite => FALSE
);END;
FOR indx IN 1 .. 10
LOOP
UTL_FILE.get_line (fid, l_line);
DBMS_OUTPUT.put_line (UTL_FILE.fgetpos (fid));
END LOOP;
UTL_FILE.fclose (fid);
END;
fgetattr_rec
BEGIN
fgetattr_t;
to UTL_FILE.FGETATTR.
UTL_FILE.fgetattr (
location => location_in,
filename => file_in,
fexists => fgetattr_rec.fexists,
file_length => fgetattr_rec.file_length,
block_size => fgetattr_rec.block_size
); flength.sql
RETURN fgetattr_rec.file_length; fileIO92.pkg
END flength;
external_tables*.*
Copyright 2000-2010 Steven Feuerstein - Page 27
External Tables 11.2 example:
get files in directory
DECLARE
l_bfile BFILE := BFILENAME ('DEMO', 'exec_ddl_from_file2.sql');
BEGIN
DBMS_OUTPUT.put_line ('Exists? ' || DBMS_LOB.fileexists (l_bfile));
DBMS_OUTPUT.put_line ('Open before open? ' || DBMS_LOB.fileisopen (l_bfile));
DBMS_LOB.fileopen (l_bfile);
DBMS_OUTPUT.put_line ('Open after open? ' || DBMS_LOB.fileisopen (l_bfile));
DBMS_LOB.fileclose (l_bfile);
DBMS_OUTPUT.
put_line ('Open after close? ' || DBMS_LOB.fileisopen (l_bfile));
END;
bfile_open_close.sql
Copyright 2000-2010 Steven Feuerstein - Page 31
Compare two BFILES with COMPARE
DECLARE
l_bfile1 BFILE := BFILENAME ('TEMP', 'file1.txt');
l_bfile2 BFILE := BFILENAME ('TEMP', 'file2.txt');
BEGIN
DBMS_LOB.fileopen (l_bfile1); DBMS_LOB.fileopen (l_bfile2);
DBMS_OUTPUT.put_line (
'Equal? ' || DBMS_LOB.compare (file_1 => l_bfile1, file_2 => l_bfile2
, amount => 33, offset_1 => 1, offset_2 => 1
)
);
bfile_demo.sql
DECLARE
l_bfile BFILE := BFILENAME ('DEMO', 'exec_ddl_from_file.sql');
l_contents RAW (32767);
l_amount PLS_INTEGER := 100;
BEGIN
DBMS_LOB.fileopen (l_bfile);
DBMS_LOB.read (l_bfile, l_amount, 1, l_contents);
dbms_output.put_line (l_contents);
DBMS_LOB.fileclose (l_bfile);
END;
dbms_output_demo.sql
Copyright 2000-2010 Steven Feuerstein - Page 38
Summary: DBMS_OUTPUT
The p package
Use "p.l" to say "show me", many overloadings of
datatypes.
The watch package
Write information to screen or database pipe
Quest Error Manager
www.ToadWorld.com/SF
More sophisticated tracing and error
management
p.pks/pkb
Copyright 2000-2010 Steven Feuerstein - Page 40 watch_noplv.pkg
Sending mail from a PL/SQL program