MariaDB创建定时事件报#1064语法错误,求排查解决
Hey there! That syntax error you're seeing near END is a classic gotcha when working with multi-statement event blocks. Let me break down what's going wrong and how to fix it.
The Root Cause
By default, MySQL/MariaDB uses the semicolon (;) as the statement delimiter. When you write a BEGIN...END block with multiple SQL statements inside, the database sees the first semicolon (after your SET statement) as the end of the entire CREATE EVENT command. The rest of the code (from UPDATE onward) then becomes invalid syntax.
The Fix: Temporary Delimiter Change
You need to temporarily switch to a different delimiter (like // or $$) so the database treats the entire BEGIN...END block as a single statement. Here's your corrected code:
-- Temporarily change the delimiter to // DELIMITER // CREATE DEFINER=`root`@`localhost` EVENT `start_game` ON SCHEDULE EVERY 3 MINUTE STARTS '2018-05-05 05:50:07' ON COMPLETION PRESERVE DISABLE DO BEGIN SET @game_id = (SELECT id FROM tbl_game WHERE game_status=0 ORDER BY id ASC LIMIT 1); UPDATE tbl_game SET game_status = 1 WHERE id = @game_id; END // -- Switch back to the default semicolon delimiter DELIMITER ;
Quick Additional Checks
- Make sure the event scheduler is enabled: Run
SHOW VARIABLES LIKE 'event_scheduler';– if the value isOFF, enable it withSET GLOBAL event_scheduler = ON;(you might need to add this to your my.cnf/my.ini to keep it enabled after restart). - Verify permissions: The
root@localhostuser should have theEVENTprivilege to create scheduled events. If not, grant it withGRANT EVENT ON *.* TO 'root'@'localhost';.
That should resolve the syntax error and get your event created properly!
内容的提问来源于stack exchange,提问作者Gurpreet Singh Matharu

