MySQL内部游标只执行一次

时间:2016-12-24 13:03:52

标签: mysql stored-procedures cursor

DELIMITER $$

CREATE PROCEDURE `remove_schedule_duplicate` ()
BEGIN
    BLOCK1 : BEGIN 

    DECLARE finished INTEGER DEFAULT 0;
    DECLARE schedule_id CHAR(36);
    DECLARE gamePk INTEGER;
    DECLARE keep_entry TINYINT(1);
    DECLARE scheduleDuplicate CURSOR FOR SELECT game_pk FROM schedule where is_active = 1 group by game_pk,home_team_id,away_team_id,venue_id having count(game_pk) > 1 limit 2;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;

    OPEN scheduleDuplicate;

    get_schedule_duplicate: LOOP

    FETCH scheduleDuplicate INTO gamePk;

    IF finished = 1 THEN
        LEAVE get_schedule_duplicate;
            END IF;

            **BLOCK2 : BEGIN 

            DECLARE block_finished INTEGER DEFAULT 0;
            DECLARE blockDuplicate CURSOR FOR SELECT id FROM schedule where game_pk = gamePk and is_active = 1;
            DECLARE CONTINUE HANDLER FOR NOT FOUND SET block_finished = 1;

            OPEN blockDuplicate;

            block_schedule_duplicate: LOOP

            FETCH blockDuplicate INTO schedule_id;

            IF block_finished = 1 THEN
                LEAVE block_schedule_duplicate;
                        END IF;

                        IF keep_entry = 0 THEN
                            UPDATE schedule set is_active = 0 where id = schedule_id;
                        END IF;

                    END LOOP block_schedule_duplicate;

                    CLOSE blockDuplicate;

            END BLOCK2;**

        END LOOP get_schedule_duplicate;

        CLOSE scheduleDuplicate;

END BLOCK1;

END 

$$

问题是Innerloop第一次执行得很好,之后block_finished总是为1。总是它退出内部区块。

如何解决此问题。我所做的 ?有人帮我解决了这个问题。

1 个答案:

答案 0 :(得分:0)

  

我已经改变了我的存储过程:

DELIMITER $$
CREATE PROCEDURE `remove_schedule_duplicate`()
BEGIN
    DECLARE finished INTEGER DEFAULT 0;
    DECLARE schedule_id,temp CHAR(36) DEFAULT NULL;
    DECLARE gamePk INTEGER;
    DECLARE keep_entry TINYINT(1);
    DECLARE scheduleDuplicate CURSOR FOR SELECT game_pk,id FROM schedule where game_pk in (SELECT game_pk FROM schedule where is_active = 1 group by game_pk,home_team_id,away_team_id,venue_id having count(game_pk) > 1) and is_active = 1 order by game_pk;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;

    OPEN scheduleDuplicate;

        get_schedule_duplicate: LOOP

        FETCH scheduleDuplicate INTO gamePk,schedule_id;

        IF finished = 1 THEN
            LEAVE get_schedule_duplicate;
        END IF;

        IF ((temp IS NULL) AND (gamePk IS NOT NULL)) OR ((temp IS NOT NULL) AND (temp <> gamePk)) THEN
            SET temp = gamePk;
            SET keep_entry = 1;
        #ELSE IF temp IS NOT NULL AND temp = gamePk THEN
        ELSE
            SET keep_entry = 0;
        END IF;

        IF keep_entry = 0 THEN
                UPDATE schedule set is_active = 0 where id = schedule_id;
        END IF;

        END LOOP get_schedule_duplicate;

    CLOSE scheduleDuplicate;

END$$
DELIMITER ;