Oracle Pl Sql Error Logging
Contents |
March 2012 Oracle Magazine Online 2016 2015 2014 2013 2012 2011 2010 January 2012 March 2012 May 2012 July 2012 September 2012 November 2012 As error table in oracle Published In March/April 2012 TECHNOLOGY: PL/SQL Error Management By Steven
Oracle Raise Exception With Message
Feuerstein Part 6 in a series of articles on understanding and using PL/SQL Even if you write pl sql exception handling examples absolutely perfect PL/SQL programs, it is possible and even likely that something will go wrong and an error will occur when those programs are run. How your code dbms_utility.format_error_backtrace example in oracle responds to and deals with that error often spells the difference between a successful application and one that creates all sorts of problems for users as well as developers. This article explores the world of error management in PL/SQL: the different types of exceptions you may encounter; when, why, and how exceptions are raised; how to define your
Oracle Sqlerrm
own exceptions; how you can handle exceptions when they occur; and how you can report information about problems back to your users. Exception Overview There are three categories of exceptions in the world of PL/SQL: internally defined, predefined, and user-defined. An internally defined exception is one that is raised internally by an Oracle Database process; this kind of exception always has an error code but does not have a name unless it is assigned one by PL/SQL or your own code. An example of an internally defined exception is ORA-00060 (deadlock detected while waiting for resource). A predefined exception is an internally defined exception that is assigned a name by PL/SQL. Most predefined exceptions are defined in the STANDARD package (a package provided by Oracle Database that defines many common programming elements of the PL/SQL language) and are among the most commonly encountered exceptions. One example is ORA-00001, which is assigned the name DUP_VAL_ON_INDEX in PL/SQL and is raised when a unique index constraint is violated. A user-defined exception is one yo
time and system resources. See Also: Oracle Database Data Warehousing Guide for more information regarding how to use DBMS_ERRLOG and Oracle Database SQL Language Reference for error logging in oracle stored procedure error_logging_clause syntax This chapter contains the following topics: Using DBMS_ERRLOG Security Model pl sql exception handling best practices Summary of DBMS_ERRLOG Subprograms Using DBMS_ERRLOG This section contains topics which relate to using the DBMS_ERRLOG package. Security Model
Pl Sql Logging Example
Security Model Security on this package can be controlled by granting EXECUTE on this package to selected users or roles. The EXECUTE privilege is granted publicly. However, to create an error logging http://www.oracle.com/technetwork/issue-archive/2012/12-mar/o22plsql-1518275.html table, you need SELECT access on the base table or view, the CREATE TABLE privilege, as well as tablespace quota for the target tablespace. Summary of DBMS_ERRLOG Subprograms Table 52-1 DBMS_ERRLOG Package Subprograms Subprogram Description CREATE_ERROR_LOG Procedure Creates the error logging table used in DML error logging CREATE_ERROR_LOG Procedure This procedure creates the error logging table needed to use the DML error logging https://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_errlog.htm capability. LONG, CLOB, BLOB, BFILE, and ADT datatypes are not supported in the columns. Syntax DBMS_ERRLOG.CREATE_ERROR_LOG ( dml_table_name IN VARCHAR2, err_log_table_name IN VARCHAR2 := NULL, err_log_table_owner IN VARCHAR2 := NULL, err_log_table_space IN VARCHAR2 := NULL, skip_unsupported IN BOOLEAN := FALSE); Parameters Table 52-2 CREATE_ERROR_LOG Procedure Parameters Parameter Description dml_table_name The name of the DML table to base the error logging table on. The name can be fully qualified (for example, emp, scott.emp, "EMP", "SCOTT"."EMP"). If a name component is enclosed in double quotes, it will not be upper cased. err_log_table_name The name of the error logging table you will create. The default is the first 25 characters in the name of the DML table prefixed with 'ERR$_'. Examples are the following: dml_table_name: 'EMP', err_log_table_name: 'ERR$_EMP' dml_table_name: '"Emp2"', err_log_table_name: 'ERR$_Emp2' err_log_table_owner The name of the owner of the error logging table. You can specify the owner in dml_table_name. Otherwise, the schema of the current connected user is used. err_log_table_space The tablespace the error logging table will be created in. If not specified, the default tablespace for the user owning the DML error logging table will be used.
Server MySQL MariaDB PostgreSQL SQLite MS Office Excel Access Word Web Development HTML CSS Color Picker Languages C Language More ASCII Table Linux UNIX Java https://www.techonthenet.com/oracle/exceptions/sqlerrm.php Clipart Techie Humor Advertisement Oracle Basics Oracle Advanced Oracle Cursors Oracle Exception Handling Named Programmer-Defined Exception Named System Exception WHEN OTHERS Clause SQLCODE SQLERRM Oracle Foreign Keys Oracle Loops/Conditionals http://www.oracle-developer.net/display.php?id=329 Oracle Transactions Oracle Triggers String/Char Functions Numeric/Math Functions Date/Time Functions Conversion Functions Analytic Functions Advanced Functions NEXT: Declare Cursor Oracle / PLSQL: SQLERRM Function This Oracle tutorial explains pl sql 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 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 pl sql exception 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: Declare Cursor Share this page: Advertisement Back to top Home | About Us | Contact Us | Testimonials | Donate While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy. We use advertisements to support this website and fund the development of new content. Copyright © 2003-2016 TechOnTheNet.com. All rights reserved.
major new feature of Oracle 10g Release 2 for bulk SQL operations. DML error logging enables us to trap "bad data" and filter it to a log table without failing our overall DML statement. This has never been possible in SQL before, although we could use complex constraint management and application code to achieve a slightly similar end-result. DML error logging is more similar in concept to the FORALL SAVE EXCEPTIONS construct in PL/SQL (new in Oracle 9i). overview of dml error logging With this feature, we can add a clause to our bulk DML statements (INSERT, UPDATE, MERGE and DELETE) to prevent the statement failing on hitting exceptions (i.e. "bad data"). Exceptional rows are added to a specifically-created errors table for investigation and/or intervention. In addition, we can control the number of bad records we will tolerate before failing the entire statement. There are two components to DML error logging as follows: LOG ERRORS clause to DML statements; and DBMS_ERRLOG package for managing error tables. We shall examine both of these components in this article, but first we will create some sample tables. getting started: sample data We will use two tables in our DML error logging examples, as follows. Note that for the examples, I created a user named EL with just CREATE SESSION, CREATE TABLE and a tablespace quota. SQL> CREATE TABLE src (x,y,z) 2 AS 3 SELECT object_id 4 , object_type 5 , object_name 6 FROM all_objects 7 WHERE ROWNUM <= 5; Table created. SQL> CREATE TABLE tgt 2 AS 3 SELECT * 4 FROM src 5 WHERE ROWNUM <= 3; Table created. SQL> ALTER TABLE tgt ADD 2 CONSTRAINT pk_tgt 3 PRIMARY KEY (x); Table altered. We have a source table (SRC) and a target table (TGT). The data is setup in such a way that a standard INSERT..SELECT from SRC into TGT will fail, as follows. SQL> INSERT INTO tgt SELECT * FROM src; INSERT INTO tgt SELECT * FROM src * ERROR at line 1: ORA-00001: unique constraint (EL.PK_TGT) violated On this basis, we can now introduce the new DML error logging feature. To begin, we will require an exceptions table. creating the error log table DML error logging works on the principle of trapping exceptions in bulk SQL statements and re-directing the "bad data" to an error table. The error table is created using an API in the new DBMS_ERRLOG package. The minimum amount of information we need to supply to this is the name of the target table we wish to trap exceptions for. Oracle will by default create an error table named "ERR$_SUBSTR(our_table_name,1,25)". If we so choose, we can optionally control the name, owner and tablespace