For internal exceptions, SQLERRM returns the message associated with the Oracle error that occurred. The message begins with the Oracle error code.
For user-defined exceptions, SQLERRM returns the message user-defined exception unless you used the pragma EXCEPTION_INIT to associate the exception with an Oracle error number, in which case SQLERRM returns the corresponding error message. For more information, see "Using SQLCODE and SQLERRM" .
SQLERRM [(error_number)]
ORA-0000: normal, successful completion
Passing a positive number to SQLERRM always returns the message
User-Defined Exception
unless you pass +100, in which case SQLERRM returns the following message:
ORA-01403: no data found
You cannot use SQLERRM directly in a SQL statement. For example, the following statement is illegal:
INSERT INTO errors VALUES (SQLERRM, ...);
Instead, you must assign the value of SQLERRM to a local variable, then use the variable in the SQL statement, as follows:
DECLARE my_sqlerrm CHAR(150); ... BEGIN ... EXCEPTION ... WHEN OTHERS THEN my_sqlerrm := SUBSTR(SQLERRM, 1, 150); INSERT INTO errors VALUES (my_sqlerrm, ...); END;
The string function SUBSTR ensures that a VALUE_ERROR exception (for truncation) is not raised when you assign the value of SQLERRM to my_sqlerrm. SQLERRM is especially useful in the OTHERS exception handler because it lets you identify which internal exception was raised.