Julien Julien - 3 months ago 24
MySQL Question

Trigger syntax error with MySQL

I'd like to create a trigger into MySQL, but I get a syntax error.
I tried to run query with PHPMyAdmin or directly from MySQL Client.

Here's 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 '' at line 5




CREATE TRIGGER asterisk.bi_queueEvents BEFORE INSERT ON aasterisk.queue_log
FOR EACH ROW
BEGIN
IF (NEW.event = 'ADDMEMBER')THEN
INSERT INTO agent_status (agentId,agentStatus,timestamp,callid) VALUES (NEW.agent,'READY',FROM_UNIXTIME(NEW.time),NULL) ON DUPLICATE KEY UPDATE agentStatus = "READY", timestamp = FROM_UNIXTIME(NEW.time), callid = NULL;
ELSIF (NEW.event = 'REMOVEMEMBER')THEN
INSERT INTO agent_status (agentId,agentStatus,timestamp,callid) VALUES (NEW.agent,'LOGGEDOUT',FROM_UNIXTIME(NEW.time),NULL) ON DUPLICATE KEY UPDATE agentStatus = "LOGGEDOUT", timestamp = FROM_UNIXTIME(NEW.time), callid = NULL;
ELSIF (NEW.event = 'PAUSE')THEN
INSERT INTO agent_status (agentId,agentStatus,timestamp,callid) VALUES (NEW.agent,'PAUSE',FROM_UNIXTIME(NEW.time),NULL) ON DUPLICATE KEY UPDATE agentStatus = "PAUSE", timestamp = FROM_UNIXTIME(NEW.time), callid = NULL;
ELSIF (NEW.event = 'UNPAUSE')THEN
INSERT INTO agent_status (agentId,agentStatus,timestamp,callid) VALUES (NEW.agent,'READY',FROM_UNIXTIME(NEW.time),NULL) ON DUPLICATE KEY UPDATE agentStatus = "READY", timestamp = FROM_UNIXTIME(NEW.time), callid = NULL;
END IF;
END ;
//

DELIMITER ;

Answer

I've found the solution, here's the good request :

delimiter |
CREATE TRIGGER bi_queueEvents
BEFORE INSERT ON queue_log
FOR EACH ROW
BEGIN
    IF NEW.event = 'ADDMEMBER' THEN
        INSERT INTO agent_status (agentId,agentStatus,timestamp,callid)
        VALUES (NEW.agent,'READY',FROM_UNIXTIME(NEW.time),NULL) ON DUPLICATE KEY UPDATE agentStatus = "READY", timestamp = FROM_UNIXTIME(NEW.time), callid = NULL;
    ELSEIF NEW.event = 'REMOVEMEMBER' THEN
        INSERT INTO agent_status (agentId,agentStatus,timestamp,callid)
        VALUES (NEW.agent,'LOGGEDOUT',FROM_UNIXTIME(NEW.time),NULL) ON DUPLICATE KEY UPDATE agentStatus = "LOGGEDOUT", timestamp = FROM_UNIXTIME(NEW.time), callid = NULL;
    ELSEIF NEW.event = 'PAUSE' THEN
        INSERT INTO agent_status (agentId,agentStatus,timestamp,callid)
        VALUES (NEW.agent,'PAUSE',FROM_UNIXTIME(NEW.time),NULL) ON DUPLICATE KEY UPDATE agentStatus = "PAUSE", timestamp = FROM_UNIXTIME(NEW.time), callid = NULL;
    ELSEIF NEW.event = 'UNPAUSE' THEN
        INSERT INTO agent_status (agentId,agentStatus,timestamp,callid)
        VALUES (NEW.agent,'READY',FROM_UNIXTIME(NEW.time),NULL) ON DUPLICATE KEY UPDATE agentStatus = "READY", timestamp = FROM_UNIXTIME(NEW.time), callid = NULL;
    END IF;
END |
delimiter ;