Mysql触发器创建失败

标签 mysql triggers creation

我在 mysql 中使用以下触发器有问题

CREATE TRIGGER item_fw_insert_trigger 
BEFORE INSERT ON item
FOR EACH ROW
BEGIN
    DECLARE ITEM_ID BIGINT;
    DECLARE COUNT INT;
    --insert an old item into the corresponding new table depending on the item type
    IF NEW.type=2 THEN
        INSERT INTO sms_item (sms_item_value,phone_number,counter,client_ip) VALUES (NEW.item,NEW.value,NEW.counter,NEW.client_ip);
    ELSEIF NEW.type=3 THEN
        INSERT INTO email_item (email_item_value,email,counter,client_ip) VALUES (NEW.item,NEW.value,NEW.counter,NEW.client_ip);
    ELSEIF NEW.type=4 OR NEW.type=5 OR NEW.type=6
        SET COUNT =(SELECT count(1) FROM tracking_item WHERE tracking_item_value=NEW.item);
        IF COUNT=0 THEN
            INSERT INTO tracking_item (tracking_tocken_value,first_name,last_name,scn,counter) VALUES
            (NEW.item,'','','',0);
        END IF;
        IF NEW.type=4 THEN
            UPDATE tracking_item SET first_name=NEW.value WHERE tracking_item_value=NEW.item; 
        ELSEIF NEW.type=5 THEN
            UPDATE tracking_item SET last_name=NEW.value WHERE tracking_item_value=NEW.item; 
        ELSEIF NEW.type=6 THEN
            UPDATE tracking_item SET scn=NEW.value WHERE tracking_item_value=NEW.item; 
        END IF;
    ELSEIF NEW.type=7 THEN
        -- to delete the phonenumbers for a tracking item we ned the id of the tracking item to have the foreign key
        SET ITEM_ID  = (SELECT tracking_item_id FROM tracking_item WHERE tracking_item_value=NEW.item);
        INSERT INTO tracking_item_phonenumbers (tracking_item_id,phone_numbers) VALUES
        (ITEM_ID,NEW.value);
    END IF;
    -- no else needes since all item types are covered
END ||

出现以下错误

ERROR 1064 (42000) at line 177: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '--insert an old item into the corresponding new table depending on the item ty' at line 7

我仔细查看了声明,但我觉得它们还不错。

最佳答案

从您的代码中删除所有 3 条注释,因为它们在语法上是错误的。

关于Mysql触发器创建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26542949/

相关文章:

SQL Server : DDL trigger, 控制表创建

java - 重构 Quartz 作业数据

java - 每个 session 多个对象与单个对象

c++ - 在 C/C++ 中创建新的证书存储

mysql - 选择特定字符串后的下 10 个字符

mysql - 对于不存在的 ID,我是否需要在 SQL 表中使用 NULL 值

php - 不显示来自mysql的数据

php - 根据表中的上次更新时间更新数据库中的数据

database - 如何从脚本创建 Access 数据库

尝试创建触发器时 Mysql 错误 #1064