创建事件在 MySQL 中出现语法错误

问题描述 投票:0回答:2

我正在尝试创建此事件,但找不到语法错误。

CREATE 
EVENT respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00' 
DO BEGIN

    -- INSERT INTO BACKUP TABLE
    INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId) 
    -- GET DATA
    SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId  
    FROM    regRawInformeAperturaLocal 
    WHERE regRawInformeAperturaLocal.date < DATE_SUB(NOW(), INTERVAL 5 MONTH);

    -- DELETE DATA FROM ORIGINAL TABLE
    DELETE FROM regRawInformeAperturaLocal WHERE regRawInformeAperturaLocal.date < DATE_SUB(NOW(), INTERVAL 5 MONTH);

END;

[代码编辑1]

CREATE 
EVENT respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00' 
DO BEGIN

    -- INSERT INTO BACKUP TABLE
    INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId) 
    -- GET DATA
    SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId  
    FROM    regRawInformeAperturaLocal 
    WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);

    -- DELETE DATA FROM ORIGINAL TABLE
    DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);

END;

[编辑] 这是语法错误消息:

[Err] 1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 11 行 '' 附近使用的正确语法

mysql database syntax-error mysql-error-1064 mysql-event
2个回答
2
投票

我可以让它发挥作用:

我必须添加 DELIMITER 并将语法从 END; 更改为 END|

无论如何,这是代码:

DELIMITER |
CREATE 
    EVENT IF NOT EXISTS respaldoRegRawInformeAperturaLocal
    ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00' 
    DO BEGIN

    -- INSERT INTO BACKUP TABLE
    INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId) 
    -- GET DATA
    SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId  
    FROM    regRawInformeAperturaLocal 
    WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);

    -- DELETE DATA FROM ORIGINAL TABLE
    DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);

END|
DELIMITER ;

现在我必须测试它,但我可以创建它,所以我想它没问题。

[编辑] 这个活动就像一个魅力。


0
投票

我在下面遇到了同样的错误:

ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 4 行 '' 附近使用的正确语法

当我在事件plus_one中没有设置

DO
语句时如下图:

CREATE EVENT plus_one
ON SCHEDULE EVERY 1 SECOND
STARTS '2023-11-15 00:00:00';

或者,当我在事件

DO
中的
plus_one
语句中没有设置任何SQL语句时,如下所示:

CREATE EVENT plus_one
ON SCHEDULE EVERY 1 SECOND
STARTS '2023-11-15 00:00:00'
DO; -- Here

于是,我用如下所示的SQL语句设置了

DO
语句,然后错误就解决了:

CREATE EVENT plus_one
ON SCHEDULE EVERY 1 SECOND
STARTS '2023-11-15 00:00:00'
DO UPDATE test SET num = num + 1; -- Here
© www.soinside.com 2019 - 2024. All rights reserved.