MySQL BEFORE INSERT TRIGGER无效

时间:2016-06-07 20:17:21

标签: mysql triggers

我有一个触发器,我想在不同的情况下更新一个名为error_id的列。我在一个名为data的数据表中插入一行,数据有一个列error_id,如果它通过了所有的情况,那么它应该有error_id等于1.这是因为某种原因无法工作..请帮忙

CREATE TRIGGER error_stamp 
BEFORE INSERT ON data
FOR EACH ROW 
BEGIN
    IF NEW.error_id = 0 THEN
        IF NEW.type_id = 4 AND NEW.date_released <= NEW.date THEN
            SET NEW.error_id = 3;
        ELSEIF NEW.type_id = 4 AND NEW.date_released < '1990-01-01' THEN
            SET NEW.error_id = 4;
        ELSEIF NEW.type_id = 4 AND NEW.date_released > DATE_FORMAT(SYSDATE(), '%Y-%m-%d') THEN
            SET NEWdata.error_id = 5;
        ELSEIF (NEW.type_id = 4 AND NEW.date_released IS NULL) THEN
            SET NEW.error_id = 6;
        ELSEIF (NEW.date IS NULL) THEN
            SET NEW.error_id = 7;
        ELSEIF (NEW.type_id = 5 AND NEW.date_released IS NULL) THEN
            SET NEW.error_id = 8;
        ELSEIF (NEW.value IS NULL) THEN
            SET NEW.error_id = 2;
        ELSE 
            SET NEW.error_id = 1;
        END IF;
    END IF;
    -- RETURN NEW; -- result is ignored since this is an AFTER trigger
END

1 个答案:

答案 0 :(得分:1)

不确定拼写错误但下面的代码块是否会导致编译错误

    ELSEIF NEW.type_id = 4 AND NEW.date_released > DATE_FORMAT(SYSDATE(), '%Y-%m-%d') THEN
        SET NEWdata.error_id = 5; <-- This line

应该是

SET NEW.error_id = 5;