Correct use of dynamic cursors in DB2 stored procedures

Source: Internet
Author: User

This article mainly introduces the correct use of dynamic cursors in DB2 stored procedures. I believe that if you have mastered the correct use of dynamic cursors in DB2 stored procedures, it will be of great help in future studies or work. The following is a detailed description of the main content of the article.

 
 
  1. CREATE PROCEDURE data_wtptest( IN in_taskid_timestamp varchar(30),  
  2. OUT o_err_no int,  
  3. OUT o_err_msg varchar(1024))  
  4. LANGUAGE SQL  
  5. P1: BEGIN ATOMIC  

Declaration start

Temporary Variable Error variable

 
 
  1. DECLARE SQLCODE integer default 0;  
  2. DECLARE SQLStmt varchar(1024) default '';  
  3. DECLARE r_code integer default 0;  
  4. DECLARE state varchar(1024) default 'AAA'; 

Record the current work of the program

 
 
  1. DECLARE at_end int DEFAULT 0;  
  2. DECLARE t_destnetid int default 0;  
  3. DECLARE t_recvid varchar(30) default '';  
  4. DECLARE SP_Name varchar(50) default 'data_integrate'; 

Declare the cursor value in the DB2 Stored Procedure

Declare Dynamic Cursor storage variables

 
 
  1. DECLARE stmt1 STATEMENT;  
  2. DECLARE c1 CURSOR FOR stmt1; 

Declarative error handling

 
 
  1. DECLARE EXIT HANDLER FOR SQLEXCEPTION  
  2. begin  
  3. set r_code=SQLCODE;  
  4. set o_err_no=1; 

Set o_err_msg = 'process [' | state | '] error,' | 'error code SQLCODE: [' | CHAR (r_code) | '].';

 
 
  1. insert into fcc_sp_log(object,name,value) values(SP_Name,in_taskid_timestamp,o_err_msg);  
  2. end;  
  3. DECLARE continue HANDLER for not found  
  4. begin  
  5. set at_end = 1; 

Set state = 'found 0 rows of records or has reached the end of the record .';

End;

Statement ended

SET state = '[add] Number of statistics in a separate test ';

 
 
  1. SET SQLStmt='SELECT count(*) FROM wtp_pre_download where task_timestamp = ?';  
  2. PREPARE stmt1 FROM SQLStmt;  
  3. OPEN c1 USING in_taskid_timestamp;  
  4. FETCH c1 INTO t_destnetid;  
  5. CLOSE c1; 

SET state = '[add] query details ';

 
 
  1. SET at_end = 0;  
  2. SET SQLStmt='SELECT recv_userid FROM wtp_pre_download where task_timestamp = ?';  
  3. PREPARE c1 FROM SQLStmt;  
  4. OPEN c1 USING in_taskid_timestamp;  
  5. SET state='[add]************';  
  6. FETCH c1 INTO t_recvid;  
  7. SET state='[add]============';  
  8. insert into fcc_sp_log(object,name,value) values(SP_Name,in_taskid_timestamp,t_recvid);  
  9. END p1 

The above content is an introduction to the use of dynamic cursors In the DB2 stored procedures. I hope you will gain some benefits.

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.