Solutions to DB2 not found exceptions

Source: Internet
Author: User

The DB2 not found exception is indeed a headache for us. We will analyze the cause and provide two solutions for this problem. We hope this will help you.

When will the DB2 not found exception occur and how the sqlcode value in db2 change?
In db2, a select statement may also encounter a not found exception, for example

 
 
  1. Declare sqlcode integer default 0;
  2. Declare SQL _code integer default 0;
  3. Declare classCode varchar (40 );
  4. Select app_class_code into classCode from kf_app_class where app_name = 'invalid records ';
  5. Set SQL _code = sqlcode;

If no record is found at this time, the sqlcode value is 100, and if yes, It is 0;
We can define not found Exception Handling

 
 
  1. declare sqlcode integer default 0;  
  2. declare sql_code integer default 0;  
  3. declare classCode varchar(40) ;  
  4. begin  
  5. declare continue handler for not found  
  6. begin  

-- Note that if not found occurs, the sqlcode must be 100.

 
 
  1. Set SQL _code = sqlcode;/* The sqlcode value here is 100 ;*/
  2. -- If you get the sqlcode value again, its value will change to 0.
  3. Set SQL _code = sqlcode;/* Here, sqlcode is changed to 0. Because the previous statement is successfully executed, sqlcode is changed to 0 */
  4. End;
  5. Select app_class_code into classCode from kf_app_class where app_name = 'invalid records ';
  6. Set SQL _code = sqlcode;/* Similarly, if no data is obtained at this time, declare continue handler is entered, and the returned sqlcode value is 0 */
  7. End;

Therefore, we can capture and process not found in two ways.
Solution to DB2 not found exception 1:

 
 
  1. Begin
  2. Declare continue handler for not found
  3. Begin
  4. -- Exception Handling Code
  5. End;
  6. SQL statement
  7. End;

DB2 not found solution 2:

 
 
  1. Begin
  2.  
  3. SQL statement
  4. If sqlcoding = 100 then
  5. -- Exception Handling Code
  6. End if;
  7. End;

Two ways to store db2 logs

Four Common DB2 cycle statements

DB2 column functions and scalar functions

DB2 Directory view description

Create a table in DB2 -- a table with an auto-incrementing Column

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.