Oracle DBA Tips and Techniques

By Arjun Raja

How to check the UTL_FILE_DIR parameter?

Posted by Arjun Raja on August 7, 2009

CREATE OR REPLACE procedure utl_file_test_write1 (
path in varchar2,
filename in varchar2,
firstline in varchar2,
secondline in varchar2)
is
output_file utl_file.file_type;
begin
output_file := utl_file.fopen (path,filename, ‘W’);

utl_file.put_line (output_file, firstline);
utl_file.put_line (output_file, secondline);
utl_file.fclose(output_file);

–exception
— when others then null;
end;
/

begin
utl_file_test_write1 (
‘/u03/users/ofsa/ofsatest/oracle_utl_data’,
‘utl_file_test’,
‘first line’,
‘second line’
);
end;
/

This works too –

SET SERVEROUTPUT ON
DECLARE
fid UTL_FILE.FILE_TYPE;
v VARCHAR2(32767);
PROCEDURE recNgo (str IN VARCHAR2)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE (‘UTL_FILE error ‘ || str);

UTL_FILE.FCLOSE (fid);
END;
BEGIN
/* Change the directory name to one to which you at least
|| THINK you have read/write access.
*/
fid := UTL_FILE.FOPEN (‘/u03/users/ofsa/ofsatest/oracle_utl_data’, ‘utl_file_test’, ‘R’);
UTL_FILE.GET_LINE (fid, v);
dbms_output.put_line (v);
UTL_FILE.FCLOSE (fid);

fid := UTL_FILE.FOPEN (‘/u03/users/ofsa/ofsatest/oracle_utl_data’, ‘utl_file_test_10’, ‘W’);
UTL_FILE.PUT_LINE (fid, v);
UTL_FILE.FCLOSE (fid);
EXCEPTION
WHEN UTL_FILE.INVALID_PATH
THEN recNgo (‘invalid_path’);
WHEN UTL_FILE.INVALID_MODE
THEN recNgo (‘invalid_mode’);
WHEN UTL_FILE.INVALID_FILEHANDLE
THEN recNgo (‘invalid_filehandle’);
WHEN UTL_FILE.INVALID_OPERATION
THEN recNgo (‘invalid_operation’);
WHEN UTL_FILE.READ_ERROR
THEN recNgo (‘read_error’);
WHEN UTL_FILE.WRITE_ERROR
THEN recNgo (‘write_error’);
WHEN UTL_FILE.INTERNAL_ERROR
THEN recNgo (‘internal_error’);
END;
/

message for success below.

first line

PL/SQL procedure successfully completed.

SQL> exit

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: