mysql 游标中可以嵌套游标吗

mysql 游标中可以嵌套游标吗,第1张

CREATE PROCEDURE SumOfStu(OUT Sdeptno int,OUT Sdeptname char(20),OUT sumBefore int,OUT sumAfter int)

BEGIN

DECLARE l_sum int

DECLARE l_Sdeptno int

DECLARE l_sno int

DECLARE l_sumBefore int

DECLARE done, done2 INT DEFAULT 0

DECLARE cur_out cursor for select SdeptNo,population from Department

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1

OPEN cur_out

REPEAT

SET l_sum=0

FETCH cur_out INTO l_Sdeptno,l_sumBefore

IF NOT done THEN

BEGIN

DECLARE cur_inner cursor for select Sno from student where SdeptNo=l_Sdeptno

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2 = 1

OPEN cur_inner

REPEAT

FETCH cur_inner into l_sno

IF NOT done2 THEN

SET ps_count = 0

SET l_sum=l_sum+1

END IF

UNTIL done2

END REPEAT

CLOSE cur_inner

SET done2 = 0

END

IF(l_sum<>l_sumBefore) THEN

update department SET population=l_sum where SdeptNo=l_Sdeptno

select l_Sdeptno INTO Sdeptno

select SdeptName INTO Sdeptname from Department where SdeptNo=l_Sdeptno

select l_sumBefore INTO sumBefore

select l_sum INTO sumAfter

end IF

END IF

UNTIL done

END REPEAT

CLOSE cur_out

END

mysql的游标是不能放到循环中创建的。你想要实现你原有功能的话,可以把第二个游标定义到另外一个存储过程中,然后在创建第二个游标的地方改成调用新建的存储过程(CALL PROC_NAME()),并且把你循环第二个游标想做的事也放到新建的存储过程里面去,就ok了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7301856.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存