我正在尝试编写一个触发器,当作业以异常快速的方式完成时(预计时间少于70%)会警告我们,以便有人可以检查是否有错误。
有一个看起来像这样的工作表:
id
machine_id
estimated_time
hour_of_completion
state (this is 0 for unfinished jobs and 1 for finished ones)
一个工作有很多子工作,所以还有一个如下所示的子工作表:
id
job_id (references job.job_id)
subjob_time (this is in seconds)
我们要记录警告的警告表:
id (auto_increment)
machine_id
hour_of_completion (this is the time of the day when the job finished)
这是我到目前为止所尝试的:
DELIMITER $$
DROP TRIGGER IF EXISTS `job_too_quick` $$
CREATE TRIGGER `job_too_quick` $$
AFTER UPDATE ON `job`
BEGIN
IF OLD.state <> NEW.state AND NEW.state = 1 THEN
IF (SELECT sum(subjob_time)
FROM subjob
WHERE job_id = OLD.id)
<
(TIME_TO_SEC(NEW.estimated_time) * 0.7) THEN
INSERT INTO warnings (machine_id, hour_of_completion)
VALUES (OLD.machine_id, OLD.hour_of_completion);
END;
END IF;
END IF;
END$$
DELIMITER ;
但是当我尝试运行它时,我收到了这个错误:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AFTER UPDATE ON `jobs`
BEGIN
IF OLD.state <> NEW.state AND NEW.s' at line 1
我觉得我必须犯一些愚蠢的语法错误,但我会疯狂地试图找出它是什么。有什么明显的东西我不见了吗?
答案 0 :(得分:0)
如果有人遇到类似的问题,那么这个错误实在是非常尴尬,因为评论中的人如此有用地指出,我错过了每一行。