创建触发器

时间:2018-02-10 09:00:45

标签: mysql database triggers mysql-error-1064

这是我的触发器,我收到MySQL语法错误。我想减少sms_index sms_count 值的余额。

 CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
  FOR EACH ROW
  BEGIN
    IF NEW.approved_status = '1' THEN
      UPDATE  sms_package SET  balance =  (balance - OLD.sms_count) WHERE  group_id = OLD.ins_group_id;
    END IF;
  END;

错误消息:

 #1064 - 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 '' at line 5 

1 个答案:

答案 0 :(得分:3)

您的代码看起来是正确的,除了分隔符可能存在的问题。请尝试以下方法:

DELIMITER //
CREATE TRIGGER sms_log_update AFTER UPDATE ON sms_index
FOR EACH ROW
BEGIN
    IF NEW.approved_status = '1' THEN
        UPDATE  sms_package SET  balance =  (balance - OLD.sms_count)
        WHERE  group_id = OLD.ins_group_id;
    END IF;
END;

DELIMITER ;

来自documentation

  

但是,就像存储例程一样,如果使用mysql程序定义执行多个语句的触发器,则需要重新定义mysql语句分隔符,以便可以使用;触发器定义中的语句分隔符。