DELIMITER $$
DROP PROCEDURE IF EXISTS `proc_cur`$$
CREATE PROCEDURE `proc_cur`()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE myid INT;
DECLARE cur1 CURSOR FOR SELECT id FROM aa;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
OPEN cur1;
REPEAT
FETCH cur1 INTO myid;
...
UNTIL done END REPEAT;
CLOSE cur1;
END$$
DELIMITER ;
注意问题:
1.定义要连在一起,否则要出错
DECLARE cur1 CURSOR FOR SELECT id FROM aa;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
2.done的值需要初始化,在循环中不需要改它的值