mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.22k stars 2.53k forks source link

SQL syntax error on ALTER EVENT with BEGIN END #2464

Closed zauker closed 3 years ago

zauker commented 3 years ago

I have to perform a query to update a MySQL Event without drop it anche create it again.

this is the query that I try to perform:

DELIMITER | 
ALTER EVENT myReset 
DO 
    BEGIN 
    UPDATE `table1` SET `row1` = NULL, `row2` = NULL, `row3` = NULL WHERE `table1`.`timestamp` <= DATE_SUB(now(), INTERVAL 30 MONTH); 
    UPDATE `table1` SET `row1` = NULL, `row2` = NULL, `row3` = NULL WHERE `table2`.`timestamp` <= DATE_SUB(now(), INTERVAL 30 MONTH); 
  END |
DELIMITER ;

but I receive this error message:

message=You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELIMITER | ALTER EVENT...

The same query works fine if performed on PhpMyAdmin

I'm using mysql Ver 15.1 Distrib 10.4.11-MariaDB, for osx10.10 (x86_64) using EditLine wrapper macOS 10.15.7 node-mysql mysql@2.18.1 node v14.15.1 npm 6.14.9

thanks in advance for any help or suggestion

dougwilson commented 3 years ago

Remove the DELIMITTER statements; those are something interpreted by your IDE and the MySQL server has no idea what that means. The SQL to pass to your MySQL server from this module should be the following:

ALTER EVENT myReset 
DO 
    BEGIN 
    UPDATE `table1` SET `row1` = NULL, `row2` = NULL, `row3` = NULL WHERE `table1`.`timestamp` <= DATE_SUB(now(), INTERVAL 30 MONTH); 
    UPDATE `table1` SET `row1` = NULL, `row2` = NULL, `row3` = NULL WHERE `table2`.`timestamp` <= DATE_SUB(now(), INTERVAL 30 MONTH); 
  END
zauker commented 3 years ago

@dougwilson thanks a lot, without the Delimiter instruction it works fine.