MySQL触发器:使用条件语句

时间:2014-11-13 11:54:44

标签: mysql triggers conditional

我有一个触发器,我终于在previous question

中工作了

我现在要做的是,在同一触发器中,如果特定字段发生更改,则更新特定字段。因此,当 step5_complete 从0更改为1时,我想将 date_completed 字段更新为当前日期时间。

我目前收到有关SQL语法错误的错误消息,但它说

  

near' //'在第13行

SET NEW.percent_complete = ( NEW.step1_complete + NEW.step2_complete + NEW.step3_complete + NEW.step4_complete + NEW.step5_complete ) * 20;

这是我的完整触发器

DROP TRIGGER IF EXISTS completed_part_after_update ;

DELIMITER //

CREATE TRIGGER completed_part_after_update
BEFORE UPDATE
   ON completed_part FOR EACH ROW

BEGIN

  IF NEW.step5_complete <> OLD.step5_complete THEN
     SET NEW.date_completed = NOW();     
  END IF;

  SET NEW.percent_complete = ( NEW.step1_complete + NEW.step2_complete + NEW.step3_complete + NEW.step4_complete + NEW.step5_complete ) * 20;

END; //

1 个答案:

答案 0 :(得分:0)

你最后的陈述看起来不对劲。它应该是

END//
DELIMITER ;

也就是说,触发器主体应该看起来像

DELIMITER //

CREATE TRIGGER completed_part_after_update
BEFORE UPDATE
   ON completed_part FOR EACH ROW

BEGIN

  IF NEW.step5_complete <> OLD.step5_complete THEN
     SET NEW.date_completed = NOW();     
  END IF;

  SET NEW.percent_complete = ( NEW.step1_complete + NEW.step2_complete + NEW.step3_complete + NEW.step4_complete + NEW.step5_complete ) * 20;

END//
DELIMITER ;