MySQL更新后触发 - 隐藏语法错误

时间:2015-04-10 11:48:49

标签: mysql triggers syntax-error

无法在我的本地测试数据库(MySQL 5.6)上创建以下触发器,我不明白为什么。

DELIMITER //

CREATE TRIGGER write_status_history AFTER UPDATE ON request
FOR EACH ROW
BEGIN
    DECLARE snew CHAR;
    DECLARE sold CHAR;
    DECLARE unew CHAR;
    DECLARE uold CHAR;

    -- Status change
    IF NEW.status <> OLD.status THEN
        SET unew = (SELECT username FROM ta_users WHERE id = NEW.ta_users_id);
        SET snew = (SELECT name FROM status WHERE id = NEW.status);
        SET sold = (SELECT name FROM status WHERE id = OLD.status);
        INSERT INTO history (`request_id`, `content`) 
        VALUES(NEW.id, CONCAT('Statuswechsel von ', sold, ' nach ', snew, ' durchgeführt von ', user, '.'));
    -- User change
    ELSE IF NEW.ta_users_id <> OLD.ta_users_id THEN
        SET unew = (SELECT username FROM ta_users WHERE id = NEW.ta_users_id);
        SET uold = (SELECT username FROM ta_users WHERE id = OLD.ta_users_id);
        INSERT INTO history (`request_id`, `content`) 
        VALUES(NEW.id, CONCAT('Bearbeiter gewechselt von ', uold, ' nach ', unew, '.'));
    END IF;
END
//

DELIMITER ;

如果我尝试启动查询,则会收到此错误消息,指示语法错误:

Error Code: 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 21.
但是,当鼠标悬停在最后Syntax Error: missing 'if'上时,MySQL Workbench会报告END

您是否知道如何修复此语法错误?我已经尝试在上一个END添加分号,这没有任何帮助 - 同样的错误。

1 个答案:

答案 0 :(得分:1)

Mysql else-if将为

elseif

当你给出一个空格时,它将考虑else,然后是另一个if,并期望内部if被关闭。