MySql存储过程循环游标 - 语法错误

时间:2013-09-23 13:23:09

标签: mysql stored-procedures

语法错误在哪里?

DECLARE irid INT DEFAULT 0;
DECLARE tmp_joinid INT DEFAULT 0;
DECLARE loopjoins_eof INT DEFAULT FALSE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET loopjoins_eof = TRUE;
START TRANSACTION;

    SET irid = (SELECT id FROM `tables` WHERE `adapter_id`=_aid AND `view_id`=_vid AND `name`=_tname);
    IF irid IS NOT NULL THEN

        DECLARE cur0 CURSOR FOR SELECT `joins`.`id` FROM `joins` WHERE `table_left_id`=irid OR `table_right_id`=irid;
        OPEN cur0;
            loopjoins: LOOP
                FETCH cur0 INTO tmp_joinid;
                IF loopjoins_eof THEN
                    LEAVE loopjoins;
                END IF;
                -- Lösche Join-Columns
                DELETE FROM `join_columns` WHERE `join_id`=tmp_joinid;
            END LOOP loopjoins;
        CLOSE cur0;

    END IF;
COMMIT;
SELECT irid;

1064 - 您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在'DECLARE cur0 CURSOR FOR SELECT joins附近使用正确的语法。id FROM joins WHERE table_left_id = i'at第12行

谢谢

2 个答案:

答案 0 :(得分:3)

一个更好的选择是避免光标,你可以用

之类的东西替换光标
DELETE FROM `join_columns`
WHERE `join_id` in 
  (SELECT `id` 
  FROM `joins` 
  WHERE `table_left_id`=irid OR `table_right_id`=irid);

答案 1 :(得分:2)

来自manual

  

游标声明必须在处理程序声明之前以及变量和条件声明之后出现