Search code examples
mysqltriggerscreation

Mysql Trigger Creation fails


I have a problem with the following trigger in 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 ||

I get the following error

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

I had a good looks at the declares but they seem fine to me.


Solution

  • Remove ALL the 3 comments away from your code because they are wrong syntaxically.