Common DB2 cycle usage

Source: Internet
Author: User

DB2 cycles are widely used in DB2 databases. The following describes four common DB2 cycle usage methods. We hope this will help you learn about DB2 loops.

1. Use the obtained cursor to cycle in the stored procedure:

 
 
  1. DECLARE CUR_FEESET cursor with return to caller (
  2. SELECT
  3. --
  4. FROM table
  5. WHERE condition
  6. );
  7. OPEN CUR_FEESET; -- get the cursor
  8. -- Obtain the number of cursor records
  9. SELECT
  10. Count (CIF_CSTNO)
  11. Into v_count
  12. FROM CB_CSTINF A, CB_CSTBSNINF B
  13. Where a. CIF_STT <> '3' and a. CIF_CSTNO = B. CBI_CSTNO ;--
  14. FETCH CUR_FEESET INTO V_CSTNO, V_FEECODE, V_CSTLEVEL, V_FEEMODE ;--
  15. WHILE V_COUNT> 0 DO
  16. ........
  17. FETCH CUR_FEESET INTO V_CSTNO, V_FEECODE, V_CSTLEVEL, V_FEEMODE ;--
  18. End while ;--
  19.  

2. Another standard db2 cycle format (leave and iterate usage ):

 
 
  1. SET V_COUNT = LENGTH (V_VALIDAUTHCOMBOS );--
  2.  
  3. SET V_INDEX = 0 ;--
  4.  
  5. AUTHLOOP:
  6. LOOP
  7. IF V_INDEX> = V_COUNT THEN
  8. Leave authloop; -- equivalent to break
  9. End if ;--
  10. ....
  11. SET V_INDEXV_INDEX = V_INDEX + 1 ;--
  12. .......
  13. IF condition THEN
  14. Iterate authloop; -- equivalent to continue
  15. End if ;--
  16. End loop ;--

3. Loop V_TEMP = 'cb1001 | CB1002 | CB1003 | ') of the truncated string '):

 
 
  1. SET V_LENGTH = LENGTH (V_TEMP );
  2. WHILE V_LENGTH> 0 DO
  3. SET V_POS = POSSTR (V_TEMP, '| ');
  4. SET V_CURRENT_BSN = SUBSTR (V_TEMP, 1, V_POS-1 );
  5. SET V_TEMP = SUBSTR (V_TEMP, V_POS + 1 );
  6. SET V_LENGTH = LENGTH (V_TEMP );
  7. -- The last field is not truncated.
  8. SET V_BSNTYPE = V_CURRENT_BSN;
  9. End while;

4. You do not need to open the cursor for a cursor loop ):

 
 
  1. DROP PROCEDURE TESTFOR;  
  2.     CREATE PROCEDURE TESTFOR()  
  3.     LANGUAGE SQL  
  4.     BEGIN  
  5.     DECLARE V_TEMP1 VARCHAR(2);  
  6.     DECLARE V_TEMP2 VARCHAR(70);  
  7.     FOR V1 AS CURSOR1 CURSOR FOR  
  8. SELECT STUDENT_ID AS TEMP1,STUDENT_NAME AS TEMP2 FROM STUDENT  
  9.     DO   
  10. DELETE FROM STUDENT WHERE STUDENT_ID = TEMP1;  
  11. SET V_TEMP1 = TEMP1;  
  12. SET V_TEMP2 = TEMP2;  
  13.     END FOR;  
  14.     COMMIT;  
  15.     END; 

Implementation of creating a database in DB2

DB2 Directory view description

Create a table in DB2 -- a table with an auto-incrementing Column

DB2 rename a table and view table information

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.