Oracle Error Handling Sqlerrm
Contents |
Server MySQL MariaDB PostgreSQL SQLite MS Office Excel Access Word Web Development HTML CSS Color oracle sqlcode list Picker Languages C Language More ASCII Table Linux UNIX Java oracle sql codes list Clipart Techie Humor Advertisement Oracle Basics Oracle Advanced Oracle Cursors Oracle Exception Handling Named
Sqlerrm Line Number
Programmer-Defined Exception Named System Exception WHEN OTHERS Clause SQLCODE SQLERRM Oracle Foreign Keys Oracle Loops/Conditionals Oracle Transactions Oracle Triggers String/Char Functions Numeric/Math Functions
Oracle Sqlcode Values
Date/Time Functions Conversion Functions Analytic Functions Advanced Functions NEXT: Declare Cursor Oracle / PLSQL: SQLERRM Function This Oracle tutorial explains how to use the Oracle/PLSQL SQLERRM function with syntax and examples. What does the SQLERRM Function do? The SQLERRM function returns the error message associated with the most sqlerrm db2 recently raised error exception. This function should only be used within the Exception Handling section of your code. Syntax The syntax for the SQLERRM function in Oracle/PLSQL is: SQLERRM Parameters or Arguments There are no parameters or arguments for the SQLERRM function. Note See also the SQLCODE function. Example Since EXCEPTION HANDLING is usually written with the following syntax: EXCEPTION WHEN exception_name1 THEN [statements] WHEN exception_name2 THEN [statements] WHEN exception_name_n THEN [statements] WHEN OTHERS THEN [statements] END [procedure_name]; You could use the SQLERRM function to raise an error as follows: EXCEPTION WHEN OTHERS THEN raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM); END; Or you could log the error to a table using the SQLERRM function as follows: EXCEPTION WHEN OTHERS THEN err_code := SQLCODE; err_msg := SUBSTR(SQLERRM, 1, 200); INSERT INTO audit_table (error_number, error_message) VALUES (err_code, err_msg); END; NEXT:
is useful only in an exception handler. Outside a handler, SQLERRM with no argument always returns the normal,
Sqlerrcode
successful completion message. For internal exceptions, SQLERRM returns the message associated sqlerrm invalid identifier with the Oracle error that occurred. The message begins with the Oracle error code. For user-defined sqlerrm length 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 https://www.techonthenet.com/oracle/exceptions/sqlerrm.php returns the corresponding error message. For more information, see "Retrieving the Error Code and Error Message: SQLCODE and SQLERRM". Syntax sqlerrm function ::= Description of the illustration sqlerrm_function.gif Keyword and Parameter Description error_number A valid Oracle error number. For a list of Oracle errors (ones prefixed by ORA-), see Oracle Database Error Messages. Usage http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/sqlerrm_function.htm Notes SQLERRM is especially useful in the OTHERS exception handler, where it lets you identify which internal exception was raised. The error number passed to SQLERRM should be negative. Passing a zero to SQLERRM always returns the ORA-0000: normal, successful completion message. Passing a positive number to SQLERRM always returns the User-Defined Exception message unless you pass +100, in which case SQLERRM returns the ORA-01403: no data found message. You cannot use SQLERRM directly in a SQL statement. Assign the value of SQLERRM to a local variable first, as shown in Example 13-6. 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 SQLERRM. Examples For examples, see the following: Example 10-11, "Displaying SQLCODE and SQLERRM" Example 13-6, "Using SQLCODE and SQLERRM" Related Topics "Exception Definition" "SQLCODE Function" Scripting on this page enhances content navigation, but does not change the content in any way.
Oracle error is no data found, in which case https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/sqlcode_function.htm SQLCODE returns +100. For user-defined exceptions, SQLCODE returns +1, https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/errors.htm or a value you assign if the exception is associated with an Oracle error number through pragma EXCEPTION_INIT. Syntax sqlcode function ::= Description of the illustration sqlcode_function.gif Usage Notes SQLCODE is only useful in an oracle sql 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 oracle error handling 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. Examples Example 13-6 shows the use of SQLCODE and SQLERRM. Example 13-6 Using SQLCODE and SQLERRM DECLARE name employees.last_name%TYPE; v_code NUMBER; v_errm VARCHAR2(64); BEGIN SELECT last_name INTO name FROM employees WHERE employee_id = 1000; EXCEPTION WHEN OTHERS THEN v_code := SQLCODE; v_errm := SUBSTR(SQLERRM, 1 , 64); DBMS_OUTPUT.PUT_LINE('The error code is ' || v_code || '- ' || v_errm); END; / For examples, see the following: Example 10-11, "Displaying SQLCODE and SQLERRM" Related Topics "Exception Definition" "SQLERRM Function" "Retrieving the Error Code and Error Message: SQLCODE and SQLERRM" Scripting on this page enhances content navigation, but does not change the content in any way.
to your PL/SQL program. With many programming languages, unless you disable error checking, a run-time error such as stack overflow or division by zero stops normal processing and returns control to the operating system. With PL/SQL, a mechanism called exception handling lets you bulletproof your program so that it can continue operating in the presence of errors. This chapter contains these topics: Overview of PL/SQL Runtime Error Handling Advantages of PL/SQL Exceptions Summary of Predefined PL/SQL Exceptions Defining Your Own PL/SQL Exceptions How PL/SQL Exceptions Are Raised How PL/SQL Exceptions Propagate Reraising a PL/SQL Exception Handling Raised PL/SQL Exceptions Overview of PL/SQL Compile-Time Warnings Overview of PL/SQL Runtime Error Handling In PL/SQL, an error condition is called an exception. Exceptions can be internally defined (by the runtime system) or user defined. Examples of internally defined exceptions include division by zero and out of memory. Some common internal exceptions have predefined names, such as ZERO_DIVIDE and STORAGE_ERROR. The other internal exceptions can be given names. You can define exceptions of your own in the declarative part of any PL/SQL block, subprogram, or package. For example, you might define an exception named insufficient_funds to flag overdrawn bank accounts. Unlike internal exceptions, user-defined exceptions must be given names. When an error occurs, an exception is raised. That is, normal execution stops and control transfers to the exception-handling part of your PL/SQL block or subprogram. Internal exceptions are raised implicitly (automatically) by the run-time system. User-defined exceptions must be raised explicitly by RAISE statements, which can also raise predefined exceptions. To handle raised exceptions, you write separate routines called exception handlers. After an exception handler runs, the current block stops executing and the enclosing block resumes with the next statement. If there is no enclosing block, control returns to the host environment. For information on managing errors when using BULK COLLECT, see "Handling FORALL Exceptions with the %BULK_EXCEPTIONS Attribute". Example 10-1 calculates a price-to-earnings ratio for a company. If the company has zero earnings, the division operation raises the predefined exception ZERO_DIVIDE, the execution of the block is interrupted, and control is transferred to the exception handlers. The o