简体   繁体   English

触发时创建计划事件的触发器 - Mysql

[英]Trigger that creates a scheduled event when is triggered - Mysql

I have a trigger that updates another table, when it is fired, also, I want to create an scheduled event which is set at the TIME that is SELECTED from the table Assignment using the foreign key relation.我有一个更新另一个表的触发器,当它被触发时,我还想创建一个预定事件,该事件设置在使用外键关系从表分配中选择的时间。

Tables looks like that:表看起来像这样:

ClassAssignment (Bridge table between Class and Assignment): ClassAssignment(Class和Assignment之间的桥接表):

classID,
assignmentID

Assignment:任务:

id
deadline DATETIME
CREATE TRIGGER newClassAssignment AFTER INSERT ON classassignment FOR EACH ROW
Begin 
INSERT INTO StudentAssignmentSolution(studentID, assignmentID) 
SELECT s.ID, NEW.assignmentID
FROM Student s
WHERE s.classID = NEW.classID;
CREATE EVENT deadlineMissed
ON SCHEDULE (select deadline from assignment where id = new.assignmentID) 
DO
update StudentAssignmentSolution set solutionDate = CURDATE() assignmentId = new.assignmentID;
END

Hope it makes sense.希望这是有道理的。 Thank you =)谢谢你=)

No, it's impossible in this form.不,这种形式是不可能的。 The event 'schedule' parameter is literal, you can't use a subquery in it.事件 'schedule' 参数是文字,您不能在其中使用子查询。 And you can't use the output generated by this subquery - the trigger can't contain dynamic SQL.而且您不能使用此子查询生成的 output - 触发器不能包含动态 SQL。

The solution:解决方案:

  • You create some service table.您创建一些服务表。
  • When your trigger is fired, it inserts the data necessary to create the event into this table.当您的触发器被触发时,它会将创建事件所需的数据插入到该表中。
  • You create a basic event procedure that is scheduled often enough.您创建了一个经常安排的基本事件过程。 This procedure takes data from the service table and, using dynamic SQL, creates the necessary event procedure.此过程从服务表中获取数据,并使用动态 SQL 创建必要的事件过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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