What types of abnormal processors are involved in DB2 stored procedures?

Source: Internet
Author: User

The following article describes how to handle exceptions in the DB2 stored procedure. In the DB2 database, if you want to use sqlcode, you must declare before the DDL statement. This is what we all need to know. The following describes the main content of this article.

Handling of Stored Procedure exceptions:

 
 
  1. DECLARE handler-type HANDLER FOR condition handler-action  

The exception Processor type (handler-type) has the following types:

After the processor operation is complete, CONTINUE continues to execute the next statement after the exception statement is generated.

After the processor operation is complete, EXIT terminates the stored procedure and returns the control to the caller.

Before UNDO executes a processor operation, DB2 rolls back the SQL operations performed during the stored procedure. After the processor operation is complete, the stored procedure is terminated and the control is returned to the caller.

Exception processors can handle custom exceptions based on specific SQLSTATE values or classes with predefined exceptions. The predefined three types of exceptions are as follows:

The not found flag causes an exception where the SQLCODE value is + 100 or the SQLSATE value is 02000. This exception usually occurs when SELECT does not return rows.

An error occurs when the SQLCODE value is negative due to the sqlexceptioin id.

The SQLWARNING flag causes a warning exception or an exception that causes a SQLCODE value other than 100.

If a not found or SQLWARNING exception is generated and no exception processor is defined for the exception, the exception is ignored and the control flow is switched to the next statement. If an SQLEXCEPTION exception is generated and no exception processor is defined for this exception, the DB2 stored procedure fails and the control flow is returned to the caller.

The following example declares two exception processors. The EXIT processor is called when an SQLEXCEPTION or SQLWARNING exception occurs. Before terminating the SQL program, the EXIT processor sets the variable named stmt to "ABORTED" and returns the control flow to the caller. The UNDO processor rolls back the SQL Operations completed in the Stored Procedure body before returning the control flow to the caller.

Listing 3: exception processor example

 
 
  1. DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING SET stmt = 'ABORTED';   
  2. DECLARE UNDO HANDLER FOR NOT FOUND;  

If the predefined exception set does not meet your requirements, you can customize the exception Declaration for the specific SQLSTATE value and then declare the processor for this custom exception. Syntax:

List 4: custom exception Processors

 
 
  1. DECLARE unique-name CONDITION FOR SQLSATE 'sqlstate'  

The processor can be defined by a separate Stored Procedure statement or by BEGIN... END Block composite statement definition. Note that the values of SQLSATE and SQLCODE are changed when the statement is executed. If you need to retain the values of SQLSATE and SQLCODE before the exception, you need to assign SQLSATE and SQLCODE to local variables or parameters in the first statement that executes the compound statement.

Generally, an output parameter (for example, poGenStatus) is defined for the DB2 stored procedure ).

 
 
  1. declare sqlcode integer default 0;   
  2. begin   
  3. declare continue handler for sqlexception set ret = sqlcode;   
  4. declare continue handler for sqlwarning set ret = sqlcode;   
  5. declare continue handler for not found set ret = sqlcode;   
  6. end ;  

Exception statement

Exception Handling

 
 
  1. If sqlcode <0 or sqlcode = 100 then
  2. Set O_RetCod = RetCode;
  3. Set O_RetMsg = 'cln02: An error occurred while connecting the product instance to the customer! ';
  4. Insert into LOG. OPER_LOG_TAB (PROC_NAME, OBJ_TAB, REGION_COD, OPER_COUNT, ERR_CODE, DATA_TIME, OPER_TIME)
  5. Values ('P _ DW_CLEAN ', 'Global TEMP', 0, 0, retcode, CHAR (last_3_mon_time), current TIMESTAMP );
  6. Return;
  7. Else
  8. Set RetCode = 0;
  9. End if;

Good instance:

 
 
  1. CREATE PROCEDURE divide ( IN numerator INTEGER,  
  2. IN denominator INTEGER,  
  3. OUT result INTEGER)  
  4. LANGUAGE SQL  
  5. BEGIN  
  6. DECLARE overflow CONDITION FOR SQLSTATE '22003';  
  7. DECLARE CONTINUE HANDLER FOR overflow  
  8. RESIGNAL SQLSTATE '22375';  
  9. IF denominator = 0 THEN  
  10. SIGNAL overflow;  
  11. ELSE  
  12. SET result = numerator / denominator;  
  13. END IF;   

The above content is an introduction to the troubleshooting methods for DB2 Stored Procedure exceptions. I hope you will find some gains.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.