标签:declare creat cal continue one state roo set create
CREATE DEFINER=`root`@`localhost` PROCEDURE `fetchdemo`()
BEGIN
DECLARE is1,ic1,isc1,ic2,isc2 INT;
DECLARE done int DEFAULT 0;
DECLARE cur1 CURSOR for select a.s1,a.c1,a.score,b.c1 ,b.score from sc a join (select * from sc where c1=02) b on a.s1=b.s1 and a.score>b.score where a.c1=01;
DECLARE CONTINUE HANDLER FOR SQLSTATE ‘02000‘ SET done = 1; //必须先定义光标,后定义HANDLER
open cur1;
fetch cur1 into is1,ic1,isc1,ic2,isc2 ;
//先FETCH一次,在打印。否则先打印,会导致最后一个记录打印两次。
REPEAT
select is1;
fetch cur1 into is1,ic1,isc1,ic2,isc2 ;
UNTIL done END REPEAT;
CLOSE cur1;
END
失败的例子在下面:
REPEAT
fetch cur1 into is1,ic1,isc1,ic2,isc2 ;//这是失败的例子,因为先FETCH,如果查询失败,则还要执行下面的SELECT 语句,会导致最后一条查询记录被执行两次。
select is1;
UNTIL done END REPEAT;
CLOSE cur1;
END
//PS:昨日喝酒,未日更。遗憾啊,第一天就这样。
标签:declare creat cal continue one state roo set create
原文地址:https://www.cnblogs.com/sdxjava/p/14044224.html