MySQL神秘的错误

时间:2014-04-03 10:58:38

标签: mysql sql loops phpmyadmin cursor

PHPMyAdmin一直告诉我,我在第41行(第三行)收到错误。我似乎无法找到它,任何帮助/建议将不胜感激。我已经仔细检查了IF-THEN语法和游标。

BEGIN
DECLARE `done` BOOLEAN DEFAULT false;
DECLARE `a` TEXT;
DECLARE `cur` CURSOR FOR SELECT `mana_cost` FROM `cards`;   
DECLARE CONTINUE HANDLER FOR NOT FOUND SET `done`=TRUE;



OPEN `cur`;

read_loop : LOOP

    FETCH `cur` INTO `a`;
    IF `done`
    THEN LEAVE read_loop;
    END IF;

    IF `a` LIKE '%{B}%'
    THEN            
        UPDATE `cards` SET `color` = 'BLACK '
        WHERE `mana_cost` = `a`;    
    ELSE IF `a` LIKE '%{G}%'
    THEN            
        UPDATE `cards` SET `color` = 'GREEN '
        WHERE `mana_cost` = `a`;    
    ELSE IF `a` LIKE '%{U}%'
    THEN            
        UPDATE `cards` SET `color` = 'BLUE '
        WHERE `mana_cost` = `a`;    
    ELSE IF `a` LIKE '%{W}%'
    THEN            
        UPDATE `cards` SET `color` = 'WHITE '
        WHERE `mana_cost` = `a`;    
    ELSE IF `a` LIKE '%{R}%'
    THEN            
        UPDATE `cards` SET `color` = 'RED '
        WHERE `mana_cost` = `a`;    

    END IF;

END LOOP;  //line 41
CLOSE `cur`;
END

2 个答案:

答案 0 :(得分:0)

你的问题非常普遍,因为MySQL在看到&#34 ;;"

时停止创建程序

你需要使用DELIMITER包装你的程序;)

DELIMITER $ 开始 DECLARE done BOOLEAN DEFAULT false; DECLARE a TEXT; DECLARE cur CURSOR FOR SELECT mana_cost FROM cards;
DECLARE CONTINUE HANDLER for NOT FOUND SET done = TRUE;

OPEN cur;

read_loop:LOOP

FETCH `cur` INTO `a`;
IF `done`
THEN LEAVE read_loop;
END IF;

IF `a` LIKE '%{B}%'
THEN            
    UPDATE `cards` SET `color` = 'BLACK '
    WHERE `mana_cost` = `a`;    
ELSE IF `a` LIKE '%{G}%'
THEN            
    UPDATE `cards` SET `color` = 'GREEN '
    WHERE `mana_cost` = `a`;    
ELSE IF `a` LIKE '%{U}%'
THEN            
    UPDATE `cards` SET `color` = 'BLUE '
    WHERE `mana_cost` = `a`;    
ELSE IF `a` LIKE '%{W}%'
THEN            
    UPDATE `cards` SET `color` = 'WHITE '
    WHERE `mana_cost` = `a`;    
ELSE IF `a` LIKE '%{R}%'
THEN            
    UPDATE `cards` SET `color` = 'RED '
    WHERE `mana_cost` = `a`;    

END IF;

END LOOP; //第41行 关闭cur; 结束 $ * DELIMITER; *

你应该尝试类似的东西,看看有关DELIMITER和MySQL笑话的文档:P

答案 1 :(得分:0)

LEAVE read_loop ;
END LOOP read_loop ;

添加循环名称和LEAVE语句。