mysql - 触发器上的语法错误

时间:2014-01-13 14:15:33

标签: mysql triggers

我正在尝试编写以下触发器:

DELIMITER $$

CREATE TRIGGER trigger_test_1 AFTER INSERT ON test FOR EACH ROW
    BEGIN
        DECLARE sID INT;
        DECLARE m INT;
        DECLARE a INT;
        DECLARE c INT;

        SET sID = NEW.s;
        SELECT MONTH(NOW()) INTO m;
        SELECT YEAR(NOW()) INTO a;

        SELECT COUNT(*) INTO c 
            FROM testt t 
            WHERE t.m = m AND t.a = a AND t.sID = sID;

        IF c = 0 THEN 
            INSERT INTO testt VALUES (m, a, sID, '', '');   
        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 '' at
line 15

表格非常简单:

CREATE TABLE test(
    s INT
);

CREATE TABLE testt(
    m INT,
    a INT,
    sID INT,
    t VARCHAR(200),
    d VARCHAR(500)
);

感谢。

1 个答案:

答案 0 :(得分:2)

你错过END IF;最后:

IF c = 0 THEN INSERT INTO testt VALUES (m, a, sID, '', ''); 
END IF;
END $$