如何用参数重复mysql游标?

时间:2017-07-03 08:08:41

标签: mysql cursor

我有跟随参数的光标。现在我需要在循环期间设置参数。这是他们这么做的一个简短方法吗?

光标:

DECLARE curs1 CURSOR FOR 
     SELECT  USER_ID,affiliate_id
 FROM `wpmr_aff_referrals` WHERE affiliate_id=AFFILIATEID;

光标正文部分:

OPEN curs1;    
read_loop : LOOP    
FETCH curs1 INTO USERID,AFFILIATEID;
IF (vCNT>=3) THEN  
set AFFILIATEID=10;
ELSE
set new.affiliate_id= 5;
END IF;    
END LOOP read_loop;
CLOSE curs1;

set AFFILIATEID=10;它应该像在Oracle中那样重新打开,我们将游标打开为c_emp (23);

1 个答案:

答案 0 :(得分:0)

尝试使用内部块和循环。

    OPEN curs1;    
        read_loop : LOOP    
        FETCH curs1 INTO USERID,AFFILIATEID;
        IF (vCNT>=3) THEN  
        set AFFILIATEID=10;
        ELSE
        set new.affiliate_id= 5;
        END IF;    

        BLOCK2: begin
                     DECLARE USERID1 varchar(50);
     DECLARE AFFILIATEID1 varchar(50) default
                    declare curs2 cursor for
                       SELECT  USER_ID,affiliate_id
         FROM `wpmr_aff_referrals` WHERE affiliate_id=AFFILIATEID;

                    open curs2;
                    LOOP2: loop
                        FETCH curs2 INTO USERID1,AFFILIATEID1;

                    end loop LOOP2;
                    CLOSE curs2;
                end BLOCK2;


        END LOOP read_loop;
        CLOSE curs1;