创建触发器

时间:2016-04-06 08:31:12

标签: mysql triggers database-trigger

我正在尝试执行以下触发器。但是,导致MySQL错误。

  

Mysql2 ::错误:您的SQL语法中有错误;查看与MySQL服务器版本对应的手册,以便在#IF;(max_dis_id IS NULL)之后使用正确的语法然后

CREATE TRIGGER add_event_display_id BEFORE INSERT ON events 
    FOR EACH ROW
    BEGIN
        DECLARE event_dis_id integer;
        DECLARE max_dis_id integer;
        IF (new.display_id IS NULL) THEN
            SELECT max(display_id) INTO max_dis_id FROM events WHERE event_type = new.event_type AND location_id = new.location_id;
            SET event_dis_id = 0
            IF (max_dis_id IS NULL) THEN
                SET max_dis_id = 1;
            ELSE
                SET max_dis_id = max_dis_id + 1;
            END IF;
            if event_dis_id > max_dis_id then
                SET new.display_id = event_dis_id;
            ELSE
                SET new.display_id = max_dis_id;
            END IF;
        END IF;
    END;

似乎语法是正确的。有人可以帮忙吗?

0 个答案:

没有答案