简体   繁体   中英

MySQL Event Schedule every last day of the month

I want to schedule a copy of a table on every last day of the month. But I get an error on code line 4. What should be the correct code? The stored procedure for the table copy works fine.

有错误的截图

DROP EVENT IF EXISTS `create_table_test_month`;
DELIMITER $$
CREATE EVENT `create_table_test_month`
ON SCHEDULE EVERY LAST_DAY(CURDATE() + INTERVAL 1 MONTH) STARTS '2022-03-31 23:30:00'
ON COMPLETION PRESERVE
DO BEGIN
call data.test_create_table();
END$$
DELIMITER ;

With kind regards,

Björn

The error is due to the fact that the EVERY clause wants an INTERVAL type (which identifies the period of time it will lag from the STARTS clause), while it's receiving a DATE value.

You can solve this issue by feeding an eligible interval in the EVERY clause, like the following:

DROP EVENT IF EXISTS `create_table_test_month`;
DELIMITER $$
CREATE EVENT `create_table_test_month`
ON SCHEDULE EVERY 1 MONTH STARTS '2022-03-31 23:30:00'
ON COMPLETION PRESERVE
DO BEGIN
    call data.test_create_table();
END$$
DELIMITER ;

For more information on events and intervals, there are some good examples at MySQL official documentation: https://dev.mysql.com/doc/refman/5.7/en/create-event.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM