Home » SQL & PL/SQL » SQL & PL/SQL » Output error message to file
Output error message to file [message #8405] Mon, 18 August 2003 08:25 Go to next message
Dave
Messages: 92
Registered: August 1999
Member
Hi y'all
I have a procedure that copies a table from one database to another. If an error occurs during the transfer, I need to write the error into a file.

My code thus far:

procedure COPY_WORKSITE
IS

fileHandler UTL_FILE.FILE_TYPE;
v_from_table_name VARCHAR2( 30) := 'PWKS_WORKSITE';
v_to_table_name VARCHAR2( 30) := 'QWPP_PPS_WORKSITE';
v_backup_table_name VARCHAR2( 30) := 'BWPP_PPS_BACKUP';
v_dblink VARCHAR2(30) := 'get_pwks';
BEGIN
FileHandler := UTL_FILE.FOPEN('temp','oracle_error.log', 'W');

-- *****************Process backup table data****************************************

blah blah blah

EXCEPTION
WHEN OTHERS THEN
-- some kind of error,
-- output error to log file
UTL_FILE.PUTF(fileHandler, 'Error: '||sqlcode||' '||sqlerrm||'n');
UTL_FILE.FCLOSE(fileHandler);

End of code.
Frstly, is this likely to work? Secondly the error I get at the moment is :
ORA-29282: invalid file ID
ORA-06512: at "SYS.UTL_FILE", line 901
ORA-06512: at "PPS_OWNER.COPY_WORKSITE", line 164
ORA-29280: invalid directory path
ORA-06512: at line 1

(Line 164 is the UTL_FILE.PUTF line)

Any ideas please, I'm stuck big time with this one.

TIA
Dave
Re: Output error message to file [message #8407 is a reply to message #8405] Mon, 18 August 2003 16:19 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
Your code works in Oracle 8i with the utl_file_dir parameter set to /temp in the init.ora file. However, since the ORA-29282 error message that you are receiving only exists in 9i, not 8i, you must be using Oracle 9i. In 9i they changed things, so that you need to create a directory object:

CREATE OR REPLACE DIRECTORY my_directory AS 'tmp';

Then you reference the directory object in upper case within your code:

FileHandler := UTL_FILE.FOPEN('MY_DIRECTORY','oracle_error.log', 'W');

Also, if you are going to use UTL_FILE.FOPEN between BEGIN and EXCEPTION, then you should include an additional UTL_FILE.FLOCSE within that same section. If the only time that you are writing to the file is within the exception block, then I would just put the UTL_FILE.FOPEN within the exception block.
Re: Output error message to file [message #8409 is a reply to message #8407] Tue, 19 August 2003 03:43 Go to previous messageGo to next message
Dave
Messages: 92
Registered: August 1999
Member
Thanks very much Barbara, that works a treat :)

Just one more thing, how do I get the current date/time in the file alongside the error message?

TIA

Dave
Re: Output error message to file [message #8421 is a reply to message #8409] Tue, 19 August 2003 15:14 Go to previous message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
UTL_FILE.PUTF
(FileHandler,
TO_CHAR (SYSDATE, 'DD-MON-YYYY HH24:MI:SS')
|| ' Error: ' || sqlcode || ' ' || sqlerrm || 'n');
Previous Topic: Instr function in Oracle
Next Topic: Help on Executing dynamic SQL...
Goto Forum:
  


Current Time: Fri Apr 26 23:22:33 CDT 2024