我正在尝试在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$$

最佳答案

根据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

10-04 11:14