Search notes:

Oracle: SQLERRM and SQLCODE

sqlerrm and sqlcode are two functions related to Oracle error messages.
Both functions cannot be used in an SQL statement.

SQLCODE

In an exception handler, sqlcode returns a number that corresponds to the exception that is being handled in the exception handler.
Values of sqlcode:

SQLERRM

The function sqlerrm(errCode) returns a string that describes the error whose code is errCode.
The maximum size of the value returned is 512 bytes.
The following block prints the error message for the SQL code -1045:
begin
   dbms_output.put_line(sqlerrm(-1045));
end;
/
ORA-01045: user ... lacks CREATE SESSION privilege; logon denied
sqlerrm can be called without passing the argument. In this case, sqlerrm prints the error message that is associated with the value of sqlcode.
Thus, calling sqlerrm without argument is only useful in an exception handler.

Misc

Oracle recommends to use dbms_utility.format_error_stack rather than sqlerrm unless the forall statement with its save exceptions clause is being used.

See also

Reading nested SQL error messages in a PL/SQL exception handler
Find Oracle error messages that match a given pattern or criteria.
Using SQLERRM to create a list of Oracle events.
Using servererror triggers to catch erroneous SQL statements.

Index