在事件中创建处理程序时出现 MySQL 错误

Getting MySQL error creating handler in an event

我正在尝试在 MySQL 5.6 中创建一个事件,但每当我尝试声明某些内容(例如参数或处理程序)时都会收到错误消息:

SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET @rollMeBack = TRUE;

我是一名 SQL 服务器人员,语法让我胃痛。我尝试像 中那样声明一个变量,但我仍然在该行收到错误。帮助我欧比旺克诺比:你是我唯一的希望。

完整(混淆)代码:

DELIMITER ||

CREATE EVENT my_event
ON SCHEDULE EVERY 5 MINUTE
ON COMPLETION PRESERVE
ENABLE
COMMENT 'Fixes status'
DO IF EXISTS (
        SELECT NULL FROM [...problem records..]
    ) THEN
        -- =================================================================================================
        -- Get a complete list of bad records:
    SET @rollMeBack = FALSE ;

        SET @LogId = 0;
        CREATE TEMPORARY TABLE updateable
            SELECT DISTINCT e.id , (@LogId:=@LogId + 1) + (SELECT MAX(dsd.id) FROM LogTable dsd ) AS LogId
            FROM Table1 e 
                INNER JOIN Table2 r
                    ON r.e_id = e.id 
                    /*Waiting 5 minutes to not interfere with current operations:*/
                    AND r.rDateTime <= CURRENT_TIMESTAMP() - INTERVAL 5 MINUTE
            WHERE e.Status = 'Sent'
                AND NOT EXISTS(SELECT NULL FROM Table2 r2 WHERE r2.e_id = e.id and r2.rDateTime is null)
            LIMIT 100 ;
        
        -- =================================================================================================
        -- =================================================================================================
        DECLARE CONTINUE HANDLER FOR SQLEXCEPTION 
            SET @rollMeBack = TRUE;

        START TRANSACTION
            -- =================================================================================================
            UPDATE Table1 as e1 
                INNER JOIN updateable AS u
                    ON e1.d=id = u.id
            SET e1.Status = 'Completed',
                e1.cDateTime = COALESCE (e1.cDateTime, (select max(rDateTime) from Table2 r5 where r5.e_id = e1.id));
            -- =================================================================================================
            -- Log updates:
            INSERT INTO Debug (ID, Message)
            SELECT u.LogId,
                'Record set to `Completed`'
            FROM updateable AS u;
            -- =================================================================================================
            -- Cleanup:
            DROP TABLE updateable;

        IF @rollMeBack = TRUE THEN
            ROLLBACK;
        ELSE
            COMMIT;
        END IF
        -- =================================================================================================
        -- =================================================================================================
    END IF; ||

分隔符;

一些修改使其可操作。

首先,必须在正确的位置声明处理程序。来自 docs:

Handler declarations must appear after variable or condition declarations.

其次,DO IF EXISTS...END IF; 应该包含一个 BEGIN...END 块,之后您可以进行处理程序声明:

...
COMMENT 'Fixes status'
DO IF EXISTS (
        SELECT NULL FROM [...problem records..]
    ) THEN
    BEGIN -- Note the added begin
        -- Moved the handler declaration to be the first statement of this block
        DECLARE CONTINUE HANDLER FOR SQLEXCEPTION 
            SET @rollMeBack = TRUE;

        -- =================================================================================================
        -- Get a complete list of bad records:
    SET @rollMeBack = FALSE ;
    ...

代码末尾:

        ...
        -- =================================================================================================
    END; -- Note the added END
END IF; ||

DELIMITER ;

顺便说一句,IF @rollMeBack = TRUE THEN...END IF 块中也存在语法错误 - END IF 末尾的分号丢失了。