Skip Headers

PL/SQL User's Guide and Reference
10g Release 1 (10.1)

Part Number B10807-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Feedback

Go to previous page
Previous
Go to next page
Next
View PDF

SQLCODE Function

The function SQLCODE returns the number code of the most recent exception.

For internal exceptions, SQLCODE returns the number of the associated Oracle error. The number that SQLCODE returns is negative unless the Oracle error is no data found, in which case SQLCODE returns +100.

For user-defined exceptions, SQLCODE returns +1, or a value you assign if the exception is associated with an Oracle error number through pragma EXCEPTION_INIT.

Syntax

Description of sqlcode_function.gif follows
Description of the illustration sqlcode_function.gif

Usage Notes

SQLCODE is only useful in an exception handler. Outside a handler, SQLCODE always returns 0. SQLCODE is especially useful in the OTHERS exception handler, because it lets you identify which internal exception was raised.

You cannot use SQLCODE directly in a SQL statement. Assign the value of SQLCODE to a local variable first.

When using pragma RESTRICT_REFERENCES to assert the purity of a stored function, you cannot specify the constraints WNPS and RNPS if the function calls SQLCODE.

Example

The following example inserts the value of SQLCODE into an audit table:

CREATE TABLE errors (code NUMBER, message VARCHAR2(128), happened TIMESTAMP);
DECLARE
   name employees.last_name%TYPE;
   my_code NUMBER;
   my_errm VARCHAR2(32000);
BEGIN
   SELECT last_name INTO name FROM employees WHERE employee_id = -1;
   EXCEPTION
      WHEN OTHERS THEN
         my_code := SQLCODE;
         my_errm := SQLERRM;
         dbms_output.put_line('Error code ' || my_code || ': ' || my_errm);
-- Normally we would call another procedure, declared with PRAGMA
-- AUTONOMOUS_TRANSACTION, to insert information about errors.
         INSERT INTO errors VALUES (my_code, my_errm, SYSTIMESTAMP);
END;
/
DROP TABLE errors;

Related Topics

Exceptions, SQLERRM Function, "Retrieving the Error Code and Error Message: SQLCODE and SQLERRM".