如何使用随机事件名称从存储过程创建事件?
我想从 MySQL 存储过程执行 CREATE EVENT sql。
- 我无法使用 PREPARE,因为 PREPARE 中不允许 CREATE EVENT,并且我想使用随机名称,因为我需要创建一个唯一的事件,并且需要在 DO 部分传递一些日期参数。
- MySQL 没有 EXEC。
SQL 的构建方式如下:
SET @sql=CONCAT('CREATE EVENT myevent',md5(concat(@dt1,@dt2)),' ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 0 SECOND
DO
call genRoutes(''',@dt1,''',''',@dt2,''');
');
生成的 @sql 查询如下所示:
CREATE EVENT myeventcadb1e41722fb3c9b2e6992e740d76ca ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 0 SECOND
DO
call genRoutes('2011-04-07 00:00:00','2011-04-14 00:00:00');
服务器版本 5.1.31-community
如何在存储过程中执行此操作?
I want to EXEC from MySQL Stored Procedure a CREATE EVENT sql.
- I cannot use PREPARE because CREATE EVENT is not allowed in PREPARE, and I want to use a random name, as I need to create a unique event, and I need to pass some date params in the DO section.
- MySQL doesn't have EXEC.
the SQL is built like this:
SET @sql=CONCAT('CREATE EVENT myevent',md5(concat(@dt1,@dt2)),' ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 0 SECOND
DO
call genRoutes(''',@dt1,''',''',@dt2,''');
');
The generated @sql query looks like:
CREATE EVENT myeventcadb1e41722fb3c9b2e6992e740d76ca ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 0 SECOND
DO
call genRoutes('2011-04-07 00:00:00','2011-04-14 00:00:00');
Server version 5.1.31-community
How to do this in Stored Procedure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不能直接执行此操作,也许您可以将对 genRoutes (或其他)的即将进行的调用存储在表中,并使用单个事件来管理下一个调用。每当您添加到即将发生的呼叫表时,如果新呼叫早于表中最早的呼叫,您可以调用更改事件以在新时间触发。当事件触发时,调用所有准备好的事件,将它们从表中删除,并更改事件以在下一个最快的调用时触发。
或者,您可以只设置一个每小时、15 分钟、5 分钟(或其他时间)触发的事件,并轮询表以查找就绪呼叫。
If you can't do it directly, perhaps you could store impending calls to genRoutes (or whatever) in a table and use a single event to manage the next call. Any time you add to the table of impending calls, if the new call is sooner than the soonest in the table, you could call alter the event to trigger at the new time. When the event triggers, call all of them that are ready, delete them from the table, and alter the event to trigger at the next soonest call.
Alternatively, you could just have an event that triggers ever hour, 15 minutes, 5 minutes (or whatever) and poll the table for ready calls.