#1064 - SQL语法错误,如何解决?

时间:2017-04-25 12:53:39

标签: mysql sql

我在我的数据库中有3个表[table_a,table_b,dataz],当我按照以下方式创建TRIGGER时,

CREATE TRIGGER trigz AFTER INSERT ON table_a
FOR EACH ROW
    IF EXISTS (SELECT * FROM table_b WHERE table_b.uid = '123') THEN
        INSERT INTO dataz
            VALUES('123');

获取ERROR

 #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

我不明白我在这里缺少什么......

我正在使用WAMP Server 2.4,有人有解决方案吗?

编辑1(对于Nigel Ren)::

SQL:

CREATE TRIGGER trigz AFTER INSERT ON table_a FOR EACH ROW 
IF EXISTS (SELECT * FROM table_b WHERE table_b.uid = 1) THEN
    BEGIN
        INSERT INTO dataz(userid) VALUES('123');
    END;
END IF; 

错误:

#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 4 

1 个答案:

答案 0 :(得分:0)

这取决于编辑承认声明结束的方式。通常它使用;;但是当程序使用时;在整个地方,您需要更改它。像

这样的东西
DELIMITER $$

CREATE TRIGGER trigz AFTER INSERT ON Bin FOR EACH ROW 
begin
IF EXISTS (SELECT * FROM Bin WHERE binID = 1) THEN
        INSERT INTO log(text) VALUES('123');
END IF; 
end;
$$

编辑: 应该使用:

DELIMITER;

最后将分隔符重置为正常。