DB2 (Procedure) stored procedure traversal loop!

The use of the first method is recommended. The biggest advantage is that it is more intuitive; in the case of many fields to be manipulated, there is no need to define too many field variables as an intermediate storage medium.

 

1. FOR method (FOR .. AS [cursor name] CURSOR FOR [SELECT...])

copy code
BEGIN -- statement block, must be added, otherwise an error will occur.
     FOR V AS MYCURSOR CURSOR FOR SELECT ID,NAME,AGE FROM PEOPLE
     DO
        BEGIN
            --Business logic processing is performed here. When each row is looped, the value of each column will be stored in the V variable
            --Example: insert data into another table
            INSERT INTO PERSON(NAME,AGE) VALUES(V.NAME,V.AGE);
        END;
     END FOR;
END;
copy code

 

2. WHILE method (DECLARE [cursor name] CURSOR FOR [SELECT.....])

copy code
BEGIN -- statement block, must be added, otherwise an error will occur.
     DECLARE NOTFOUND INT DEFAULT 0;
     DECLARE V_NAME VARCHAR(20);
     DECLARE V_AGE VARCHAR(20);

     DECLARE MYCURSOR CURSOR FOR SELECT NAME,AGE FROM PEOPLE;
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET NOTFOUND = 1;
     WHILE NOTFOUND = 0 DO FETCH MYCURSOR INTO V_NAME,V_AGE -- the order here should be consistent with the query result field order
        --There will be one more loop here, so you need to add a judgment
        IF(NOTFOUND = 0)
        THEN
            INSERT INTO PERSON(NAME,AGE) VALUES(V_NAME,V_AGE);
        END IF;
     END WHILE;
 END; 

Copied from netizens, original address: https://www.cnblogs.com/javalism/p/3511742.html

The use of the first method is recommended. The biggest advantage is that it is more intuitive; in the case of many fields to be manipulated, there is no need to define too many field variables as an intermediate storage medium.

 

1. FOR method (FOR .. AS [cursor name] CURSOR FOR [SELECT...])

copy code
BEGIN -- statement block, must be added, otherwise an error will occur.
     FOR V AS MYCURSOR CURSOR FOR SELECT ID,NAME,AGE FROM PEOPLE
     DO
        BEGIN
            --Business logic processing is performed here. When each row is looped, the value of each column will be stored in the V variable
            --Example: insert data into another table
            INSERT INTO PERSON(NAME,AGE) VALUES(V.NAME,V.AGE);
        END;
     END FOR;
END;
copy code

 

2. WHILE method (DECLARE [cursor name] CURSOR FOR [SELECT.....])

copy code
BEGIN -- statement block, must be added, otherwise an error will occur.
     DECLARE NOTFOUND INT DEFAULT 0;
     DECLARE V_NAME VARCHAR(20);
     DECLARE V_AGE VARCHAR(20);

     DECLARE MYCURSOR CURSOR FOR SELECT NAME,AGE FROM PEOPLE;
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET NOTFOUND = 1;
     WHILE NOTFOUND = 0 DO FETCH MYCURSOR INTO V_NAME,V_AGE -- the order here should be consistent with the query result field order
        --There will be one more loop here, so you need to add a judgment
        IF(NOTFOUND = 0)
        THEN
            INSERT INTO PERSON(NAME,AGE) VALUES(V_NAME,V_AGE);
        END IF;
     END WHILE;
 END; 

Copied from netizens, original address: https://www.cnblogs.com/javalism/p/3511742.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325305768&siteId=291194637