DB2 cursor and usage

Source: Internet
Author: User

To learn how to use a DB2 database, DB2 cursors must be mastered. We will introduce the definition and usage of DB2 cursors in detail and give examples to help you understand them.

Define a cursor:
Declare cursor name CURSOR
Select statement;

Open cursor:
OPEN cursor name;

Valid value:
FETCH cursor name INTO Variable list

Example:

 
 
  1. DECLARE c1 CURSOR FOR  
  2. SELECT CAST(salary AS DOUBLE)  
  3. FROM staff  
  4. WHERE DEPT = deptNumber 
  5. ORDER BY salary;  
  6. DECLARE EXIT HANDLER FOR NOT FOUND  
  7. SET medianSalary = 6666;  
  8. SET medianSalary = 0;  
  9.  
  10. SELECT COUNT(*) INTO v_numRecords  
  11. FROM staff  
  12. WHERE DEPT = deptNumber;  
  13. OPEN c1;  
  14. WHILE v_counter < (v_numRecords / 2 + 1) DO  
  15. FETCH c1 INTO medianSalary;  
  16. SET v_counterv_counter = v_counter + 1;  
  17. END WHILE;  
  18. CLOSE c1;  
  19.  

Note: If the cursor declaration is placed in the middle segment, use "begin... End; ". segment segmentation Mark split.
 

How to manage DB2 tablespaces

DB2 database Permissions

Quick clearing of big data DB2 tables

Silent status of three DB2 tables

Troubleshooting of DB2 database detachment

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.