Multi-Statement Events
For complex logic, wrap the event body in BEGIN...END with a DELIMITER change so MySQL does not terminate the statement early.
For complex logic, wrap the event body in BEGIN...END with a DELIMITER change so MySQL does not terminate the statement early.
DELIMITER //
CREATE EVENT monthly_archive
ON SCHEDULE EVERY 1 MONTH
STARTS "2024-02-01 01:00:00"
DO
BEGIN
INSERT INTO orders_archive SELECT * FROM orders WHERE created_at < NOW() - INTERVAL 6 MONTH;
DELETE FROM orders WHERE created_at < NOW() - INTERVAL 6 MONTH;
INSERT INTO event_log (event_name, run_at) VALUES ("monthly_archive", NOW());
END//
DELIMITER ;
Change the DELIMITER before multi-statement events or procedures to avoid premature statement termination.