我在MySQL Workbench 8.0.12社区,win10中创建了如下程序。声明游标循环遍历一个表中超过 16000 个成员(member) ID,将其在不同日期创建的订单数插入到另一个表中。
此处为光标选择成员(member) ID 及其注册日期。 从 DICT_ALLE_AAAF 中选择不同的 BAG,AAA,其中 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 ...
关于mysql - 为什么 MySQL 游标在报告成功时停止循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58875842/