为什么MySQL 报成功时光标停止循环

Why MySQL Cursor stopped looping while reporting a success

我在MySQL workbench 8.0.12 community, win10 中创建了如下程序。声明游标循环遍历table中的16000多个会员ID,将他们在不同日期创建的订单数插入到另一个table.

此处光标选择会员ID和注册日期。 select distinct BAG,AAA from DICT_ALLE_AAAF where AAA>=Date_Memb_Star。一旦我调用它call PRO_MIDD_MEMB_AAAA_1('2017/01/01','2019/11/10',60),它只循环了5个成员的ID并且没有报错。但是,如果我将 while 循环从过程中取出并打开 select Var_BAG,则游标循环会选择 200 多个成员的 ID,直到它要求我取消它。谁能告诉我我的代码有什么问题?非常感谢。

delimiter //
create procedure PRO_MIDD_MEMB_AAAA_1
(in Date_Memb_Star date,
in Date_Sale_End date,
in Int_Inte int)
begin

    declare Int_Floo int default 0;
    declare Int_Ceil int;
    declare Int_MembAll int;
    declare Int_Loop int default 1;
    declare Int_MaxiLoop int;
    declare Int_DaySpen int default 30;

    declare Int_Done int;
    declare Var_BAG varchar(16);
    declare Date_Memb_Regi date;
    declare Cur_Memb cursor for 
    select distinct BAG,AAA from DICT_ALLE_AAAF where AAA>=Date_Memb_Star order by BAG;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET Int_Done = 1;

    truncate table MIDD_MEMB_AAAA;

    open Cur_Memb;
    read_loop:loop
        fetch Cur_Memb into Var_BAG,Date_Memb_Regi;
        if Int_Done=1 then
            leave read_loop;
        end if;

        /*select Var_BAG;*/

        set Int_MaxiLoop=ceil(datediff(Date_Sale_End,Date_Memb_Regi)/Int_Inte);

        set Int_Floo=0;

            while Int_Loop<=Int_MaxiLoop do  

            set Int_Ceil=Int_Floo+Int_Inte;

            insert into MIDD_MEMB_AAAA
            select Var_BAG,Int_Floo,Int_Ceil, 
            count(distinct BAK)*(Int_DaySpen/Int_Inte) as Numb_Con_Avg
            from OPER_SALE_AAAB 
            where BAG=Var_BAG
            and timestampdiff(hour,Date_Memb_Regi,AAA)/24>Int_Floo 
            and timestampdiff(hour,Date_Memb_Regi,AAA)/24<=Int_Ceil
            and AAB>0;

            set Int_Floo=Int_Floo+Int_Inte;

            set Int_Loop=Int_Loop+1;
        end while;   

        end loop read_loop;

    close Cur_Memb;
end//
delimiter ;

小疏忽:您忘记重置变量 Int_Loop,因此在第一个 运行 之后,它仅在 Int_MaxiLoop 具有新的最大值时才进入循环。

在循环之前重新初始化它(我假设为 1):

    ...
    set Int_Floo=0;
    set Int_Loop=1;
    while Int_Loop<=Int_MaxiLoop do  ...