2

I'm trying to create this event but I can't find the syntax error.

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;

[CODE EDIT 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;

[EDIT] This is the syntax error message:

[Err] 1064 - 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 '' at line 11

15
  • 1
    post the error .. Commented Aug 3, 2017 at 15:18
  • Updated. It says it's on this string: "WHERE regRawInformeAperturaLocal.date < DATE_SUB(NOW(), INTERVAL 5 MONTH);" Commented Aug 3, 2017 at 15:19
  • why is regRawInformeAperturaLocal.date, not only date ? Commented Aug 3, 2017 at 15:21
  • Not that it matters, but why are you referencing the table name in the Where clause when you don't need to? Commented Aug 3, 2017 at 15:21
  • 1
    Have you tried using DELIMITER?. Commented Aug 3, 2017 at 15:38

2 Answers 2

2

I could make it work:

I have to add the DELIMITER and change the syntaxis from END; to END|

Anyway here's the code:

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 ;

Now I have to test it but I could create it so I guess It's fine.

[EDIT] The event works like a charm.

Sign up to request clarification or add additional context in comments.

1 Comment

Using a delimiter solved the issue for me on Amazon RDS with MySQL v5.7
1

I got the same error below:

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 '' at line 4

When I didn't set DO statement in the event plus_one as shown below:

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

Or, when I didn't set any SQL statements in DO statement in the event plus_one as shown below:

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

So, I set DO statement with a SQL statement as shown below, then the error was solved:

CREATE EVENT plus_one
ON SCHEDULE EVERY 1 SECOND
STARTS '2023-11-15 00:00:00'
DO UPDATE test SET num = num + 1; -- Here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.