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
- Declare sqlcode integer default 0;
- Declare SQL _code integer default 0;
- Declare classCode varchar (40 );
- Select app_class_code into classCode from kf_app_class where app_name = 'invalid records ';
- 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
- declare sqlcode integer default 0;
- declare sql_code integer default 0;
- declare classCode varchar(40) ;
- begin
- declare continue handler for not found
- begin
-- Note that if not found occurs, the sqlcode must be 100.
- Set SQL _code = sqlcode;/* The sqlcode value here is 100 ;*/
- -- If you get the sqlcode value again, its value will change to 0.
- Set SQL _code = sqlcode;/* Here, sqlcode is changed to 0. Because the previous statement is successfully executed, sqlcode is changed to 0 */
- End;
- Select app_class_code into classCode from kf_app_class where app_name = 'invalid records ';
- 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 */
- End;
Therefore, we can capture and process not found in two ways.
Solution to DB2 not found exception 1:
- Begin
- Declare continue handler for not found
- Begin
- -- Exception Handling Code
- End;
- SQL statement
- End;
DB2 not found solution 2:
- Begin
-
- SQL statement
- If sqlcoding = 100 then
- -- Exception Handling Code
- End if;
- 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