1

语法错误在哪里?

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 附近使用正确的语法joinsidFROM joinsWHERE table_left_id=i' 在第 12 行

谢谢

4

2 回答 2

3

一个更好的选择是避免使用光标,您可以将光标替换为类似

DELETE FROM `join_columns`
WHERE `join_id` in 
  (SELECT `id` 
  FROM `joins` 
  WHERE `table_left_id`=irid OR `table_right_id`=irid);
于 2013-09-23T16:20:06.967 回答
2

手册

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

于 2013-09-23T13:29:51.530 回答