There are two built-in Oracle PL/SQL functions that return the error message associated with the error code of the most recently raised exception in your PL/SQL block. The first function is SQLERRM. The second is the less known function DBMS_UTILITY.FORMAT_ERROR_STACK.
Which one to use? To answer this question, let us first review the details of each function.
SQL> BEGIN
2 DBMS_OUTPUT.put_line (SQLERRM (-1));
3 END;
4 /
ORA-00001: unique constraint (.) violated
PL/SQL procedure successfully completed.
SQL> BEGIN
2 RAISE NO_DATA_FOUND;
3 EXCEPTION
4 WHEN OTHERS
5 THEN
6 DBMS_OUTPUT.put_line (SQLERRM);
7 END;
8 /
ORA-01403: no data found
PL/SQL procedure successfully completed.
SQL> BEGIN
2 DBMS_OUTPUT.put_line (SQLERRM);
3 DBMS_OUTPUT.put_line (SQLERRM(0));
4 END;
5 /
ORA-0000: normal, successful completion
ORA-0000: normal, successful completion
PL/SQL procedure successfully completed.
SQL> BEGIN 2 DBMS_OUTPUT.put_line (SQLERRM(1)); 3 END; 4 / User-Defined Exception PL/SQL procedure successfully completed.
SQL> BEGIN
2 DBMS_OUTPUT.put_line (SQLERRM (7));
3 DBMS_OUTPUT.put_line (SQLERRM (-9));
4 END;
5 /
-7: non-ORACLE exception
ORA-00009: Message 9 not found; product=RDBMS; facility=ORA
PL/SQL procedure successfully completed.
SQL> BEGIN
2 RAISE NO_DATA_FOUND;
3 EXCEPTION
4 WHEN OTHERS
5 THEN
6 DBMS_OUTPUT.put_line (DBMS_UTILITY.FORMAT_ERROR_STACK);
7 END;
8 /
ORA-01403: no data found
PL/SQL procedure successfully completed.
SQL> BEGIN
2 DBMS_OUTPUT.put_line (
3 COALESCE(DBMS_UTILITY.format_error_stack,'I am NULL'));
4 END;
5 /
I am NULL
PL/SQL procedure successfully completed.
DBMS_UTILITY.FORMAT_ERROR_STACK will return is 2000 characters.Because of the size restriction (512 versus 2000 characters), it is recommended that you call DBMS_UTILITY.FORMAT_ERROR_STACK instead of SQLERRM to ensure that you see the full error message string.
However, you can use SQLERRM to check whether a particular number is a valid Oracle error or to return the error message corresponding to a particular error code.
Related articles:
Filed in Oracle on 01 Aug 06 | Tags: exception, pl/sql
I think this article misses the real difference between SQLERRM and FORMAT_ERROR_STACK.
The latter returns the entire error stack (the clue is in the name). Sometimes the real error you want to identify is some way down the error stack – in which case SQLERRM is no good for you.
If looking for a particular error number in FORMAT_ERROR_STACK you can simply do a:
EXCEPTION WHEN OTHERS THEN IF INSTR(dbms_utility.format_error_stack, ‘ORA-12345′) != 0 THEN … ELSE RAISE; END;
That should, of course, have ended with
ELSE RAISE; END IF; END;
Does anyone of you knows how to do the same in c. You pass an error code and you get the message like perror in Unix.
Thanks
@Matt Good point. Thanks for the tip.
@Guillaume I’m afraid I cannot help you with C.
hi i am preparing for job interviews as a pl/sql developer. where can i find good interview questions on database triggers and cursors. ne pointers to tat plzzz