DB2 programming skills-correct use of cursors

Source: Internet
Author: User

Today we will mainly talk about the use of the cursor in the tips of DB2 programming. If you are interested in the use of the cursor in the tips of DB2 programming, you can click to view the following articles. The following is a detailed description of the main content of the article. I hope you will have a better understanding of it after browsing.

Note: commit and rollback

When using a cursor, pay special attention that if the with hold option is not added, the cursor will be closed during Commit and Rollback. There are many things to note about Commit and Rollback. Be careful

Two Methods for defining a cursor

One is

 
 
  1. declare continue handler for not found  
  2. begin  
  3. set v_notfound = 1;  
  4. end;  
  5. declare cursor1 cursor with hold for select market_code from tb_market_code for update;  
  6. open cursor1;  
  7. set v_notfound=0;  
  8. fetch cursor1 into v_market_code;  
  9. while v_notfound=0 Do  
  10. --work  
  11. set v_notfound=0;  
  12. fetch cursor1 into v_market_code;  
  13. end while;  
  14. close cursor1;  

This method is complex but flexible. In particular, you can use the with hold option. This method can only be used if the cursor is not closed due to a commit or rollback in a loop.

The other is

 
 
  1. pcursor1: for loopcs1 as cousor1 cursor as  
  2. select market_code as market_code  
  3. from tb_market_code  
  4. for update  
  5. do  
  6. end for;  

The advantage of this method is that it is relatively simple and does not need or allow) use open, fetch, close.

However, the with hold option cannot be used. If you want to use commit in a cursor loop, rollback cannot use this method. If you do not have the commit or rollback requirements, we recommend this method (it seems that there is a problem with the For method ).

How to modify the current record of a cursor

 
 
  1. update tb_market_code set market_code='0' where current of cursor1; 

However, be sure to define cursor1 as a modifiable cursor.

 
 
  1. declare cursor1 cursor for select market_code from tb_market_code  
  2. for update;  

For update cannot be used together with group by, DISTINCT, order by, for read only, UNION, EXCEPT for union all.

The above content is a description of the usage of the cursor in the tips of the DB2 compiler. I hope it will help you in this regard.

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.