1

What is the right method to reuse a cursor inside a mysql stored procedure?

We're doing it like below, and It just does not look correct. Does anyone know whats the right method to do this?

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 ;

Heres the question that started it: While loop in stored procedure loops only twice instead of 8 times

4

0 回答 0