MySQL 5.6 - Creating an event inside a stored procedure
-
29-09-2020 - |
Question
I am trying to create a procedure which create an event in MySQL.
For example, something similar to this:
delimiter //
CREATE PROCEDURE schedule_my_db_job()
BEGIN
CREATE EVENT MY_DB_JOB
ON SCHEDULE EVERY 1 MINUTE DO
BEGIN
SELECT * FROM information_schema.events; -- for example
END;
END//
delimiter ;
But it gives me following error when I try to create this procedure.
ERROR 1576 (HY000): Recursion of EVENT DDL statements is forbidden when body is present
As I understand MySQL allow stored procedures to have CREATE EVENT sql statement inside. (Quotation from https://dev.mysql.com/doc/refman/5.6/en/create-event.html "You can create an event as part of a stored routine, but an event cannot be created by another event")
Could you please let me know what is the error here?
Thanks.
Solution
It seems there is a conflict in MySQL documentation. I think CREATE EVENT Syntax section in MySQL documentation is not correctly updated in later versions. Although "CREATE EVENT Syntax" sections says
You can create an event as part of a stored routine, but an event cannot be created by another event.
In Event Scheduler Restrictions section in mysql documentation, It says
An event may not be created, altered, or dropped by a stored routine, trigger, or another event. An event also may not create, alter, or drop stored routines or triggers. (Bug #16409, Bug #18896)