UrKll
UrKll

Reputation: 993

SQL Error #1064 while Create Trigger

I'm trying to create an Trigger within my MySQL Database, but I always get the 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 'BEGIN DECLARE tmppl INT; SELECT MAX(PortListID) FROM pj_servers INTO t' at line 2

This is the query. I just can't find the error.

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$$

Upvotes: 0

Views: 106

Answers (2)

UrKll
UrKll

Reputation: 993

OK found it, dnll was right with the FOR EACH ROW but also I tried to make the Insert with an WHERE statement, wich clearly couldn't work.

fixed code is:

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 

Upvotes: 0

dnll
dnll

Reputation: 677

According to the 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 }

so probably you're missing FOR EACH ROW in the first line

Upvotes: 1

Related Questions