这个MySQL Trigger有什么问题可以防止空日期?

时间:2014-01-13 15:51:30

标签: mysql sql triggers

我正在尝试创建此触发器以防止插入空日期:

CREATE TRIGGER responses_before_insert BEFORE INSERT ON responses
FOR EACH ROW
BEGIN
IF (NEW.date_of_plan IS NULL OR NEW.date_of_plan = '0000-00-00') THEN
SET NEW.date_of_plan = CURDATE();
END IF;
IF (NEW.date_of_update IS NULL OR NEW.date_of_update = '0000-00-00') THEN
SET NEW.date_of_update = CURDATE();
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 

任何人都可以解释什么是错的吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

通过在查询之前更改分隔符来解决问题,即。我执行了

DELIMITER $$

在触发查询之前。

答案 1 :(得分:0)

就像你一样,你需要一个分隔符。

DELIMITER $$
CREATE TRIGGER responses_before_insert BEFORE INSERT ON responses
FOR EACH ROW
BEGIN
IF (NEW.date_of_plan IS NULL OR NEW.date_of_plan = '0000-00-00') THEN
SET NEW.date_of_plan = CURDATE();
END IF;
IF (NEW.date_of_update IS NULL OR NEW.date_of_update = '0000-00-00') THEN
SET NEW.date_of_update = CURDATE();
END IF;
END $$
DELIMITER ;

然后您也可以将其设置为最后一行。更好的实践!