创建触发器时出现SQL错误#1064

时间:2014-02-10 17:40:57

标签: mysql sql

我正在尝试在我的MySQL数据库中创建一个触发器,但我总是得到错误:

  

#1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在第2行“BEGIN DECLARE tmppl INT; SELECT MAX(PortListID) FROM pj_servers INTO t”附近使用正确的语法

这是查询。 我只是找不到错误。

CREATE TRIGGER init_new_server AFTER INSERT ON pj_servers
BEGIN
    DECLARE tmppl INT;
    SELECT MAX(PortListID) FROM pj_servers INTO tmppl;

    INSERT INTO pj_servers (Registered, PortListID)
    VALUES (Current_Timestamp, (tmppl+1))
    WHERE pj_servers.ID = NEW.ID;

    INSERT INTO pj_serverports (PortList, Port, isOpen, Script)
    VALUES (tmppl+1, 80, TRUE, "say(\"Hallo\"");
END$$

2 个答案:

答案 0 :(得分:1)

根据MySQL Reference Manual

CREATE
    [DEFINER = { user | CURRENT_USER }]
    TRIGGER trigger_name
    trigger_time trigger_event
    ON tbl_name FOR EACH ROW
    trigger_body

trigger_time: { BEFORE | AFTER }

trigger_event: { INSERT | UPDATE | DELETE }

所以你可能错过了第一行的FOR EACH ROW

答案 1 :(得分:0)

确定找到它,dnll与FOR EACH ROW一样正确但我也尝试用Insert语句制作WHERE,这显然无效。

固定代码是:

CREATE TRIGGER init_new_server AFTER INSERT ON pj_servers
FOR EACH
ROW BEGIN
DECLARE tmppl INT;

SELECT MAX( PortListID )
FROM pj_servers
INTO tmppl;

UPDATE pj_servers SET Registered = CURRENT_TIMESTAMP ,
PortListID = tmppl +1 WHERE pj_servers.ID = NEW.ID;

INSERT INTO pj_serverports( PortList, Port, isOpen, Script )
VALUES (
tmppl +1, 80,
TRUE , "say(\"Hallo\")"
);

END