Two pointer loops in the DB2 Stored Procedure

Source: Internet
Author: User

I believe everyone knows about the DB2 stored procedure. The following describes two pointer loops in the DB2 stored procedure. I hope this will help you learn about the DB2 stored procedure.

Pointer loops in DB2 stored procedures:

There are two ways for DB2 stored procedures: for Loop and while LOOP, for example:

For Loop:

 
 
  1. for c1 as select deliveryid,deliverycode from delivery where status=40 for read only do  
  2.  
  3. select sum(qty) into dQty from deliverydetail where deliveryid=c1.deliveryid;  
  4.  
  5. ......  
  6.  
  7. end for;  

While loop:

 
 
  1. declare c1 cursor for select deliveryid,deliverycode from delivery where status=40 for read only ;  
  2.  
  3. DECLARE CONTINUE HANDLER FOR NOT FOUND SET at_end_c1 = 1;  
  4.  
  5. set at_end_c1=0;  
  6. open c1 ;  
  7. FETCH c1 INTO ndeliveryid ,sdeliverycode;  
  8. WHILE ( at_end_c1 = 0) do  
  9.      select sum(qty) into dQty from deliverydetail where deliveryid=ndeliveryid ;  
  10.  
  11. ...  
  12.  
  13. set at_end_c1=0;  
  14.     FETCH c1 INTO ndeliveryid ,sdeliverycode;  
  15. END while ;  
  16. close c1;  

Obviously, the for loop is much simpler than the while loop.

In addition, if you do not pay attention to the while loop, it is easy to see an endless loop.

We recommend that you use a for loop.

However, sometimes the while LOOP must be used.

If there is a transaction in the loop, for example, there is a transaction that requires independent transactions in the loop, if the execution is successful, the transaction will be committed. If the execution fails, the transaction will be rolled back, to continue the next loop, you can only use the while loop.

Because the current pointer is automatically closed when commit is used.

In the while LOOP, if you want to commit without closing the current pointer, add with hold when defining the pointer. The above pointer is defined:

 
 
  1. declare c1 cursor with hold for select deliveryid,deliverycode from delivery where status=40 for read only ; 

There is no way to deal with the for loop.

To use rollback, You need to define the savepoint and then roll back to the specified rollback point.
 

Implementation of creating a database in DB2

DB2 Directory view description

DB2 database commands

DB2 mount command performance factors

Implementation of DB2 column-to-row

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.