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了欢迎分享,转载请注明来源:内存溢出
评论列表(0条)