在mysql存储过程中重用游标的正确方法是什么?

时间:2013-05-20 15:27:25

标签: mysql sql stored-procedures

在mysql存储过程中重用游标的正确方法是什么?

我们就像下面这样做,它看起来不正确。有谁知道这是做正确的方法吗?

DELIMITER $$

USE `test`$$

DROP PROCEDURE IF EXISTS `fred`$$

CREATE DEFINER=`root`@`windows7.home` PROCEDURE `fred`()
BEGIN
    DECLARE X INT;
DECLARE done INT DEFAULT FALSE;
    DECLARE myType INT;
    DECLARE cur1 CURSOR FOR SELECT val FROM checkval;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    SET X = 10;
    WHILE X  >= 2 DO
##############
    OPEN cur1;
    FETCH cur1 INTO myType;
    read_loop: LOOP
        IF done THEN
            LEAVE read_loop;
        END IF;
        INSERT INTO myType VALUES (myType);
        FETCH cur1 INTO myType;
    END LOOP read_loop;                              
CLOSE cur1;
        SET  X = X-1;
        SET done=FALSE;
    ##################
    END WHILE;
    END$$

DELIMITER ;

这是启动它的问题: While loop in stored procedure loops only twice instead of 8 times

0 个答案:

没有答案