Mysql触发器创建失败

时间:2014-10-24 06:57:29

标签: 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 ||

我收到以下错误

  

第177行的错误1064(42000):您的SQL语法出错;   查看与您的MariaDB服务器版本对应的手册   正确的语法使用' - 插入旧项目附近   相应的新表取决于第7行的项目''

我对宣言很了解,但对我来说似乎很好。

1 个答案:

答案 0 :(得分:0)

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