无法获得Mysql触发器声明

时间:2012-02-24 13:26:14

标签: mysql triggers

有人可以告诉我这个触发器声明有什么问题吗?

DELIMITER //

CREATE TRIGGER something AFTER INSERT ON sometable
FOR EACH ROW
BEGIN
DECLARE var INT DEFAULT 0;
SET var = SELECT COUNT(*) FROM anothertable;
IF var=0 THEN
INSERT INTO anothertable values(`x`,`y`,`z`);
END IF;
END//

我一直收到错误,说我有语法错误......

1 个答案:

答案 0 :(得分:1)

尝试将SELECT语句放在括号中:

DELIMITER //

CREATE TRIGGER something AFTER INSERT ON sometable
FOR EACH ROW
BEGIN
    DECLARE var INT DEFAULT 0;
    SET var = (SELECT COUNT(*) FROM anothertable);
    IF var=0 THEN
        INSERT INTO anothertable values('x','y','z');
   END IF;
END//