Search code examples
mysqlsyntaxtriggersdatabase-trigger

MySQL Create Trigger Syntax Error (Last Line)


I'm creating a MySQL trigger designed to update various tables with a new value if certain values are changed by an UPDATE query. I keep receiving the following syntax error for this particular trigger:

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 29

Line 29 in this case is the line of the END statement.

This is my full code:

DELIMITER $$
CREATE TRIGGER update_selling_prices BEFORE UPDATE ON t1
FOR EACH ROW
BEGIN
    DECLARE update_price INT DEFAULT 0;
    DECLARE selling_price_1 DECIMAL(10, 3) DEFAULT 0.000;
    DECLARE selling_price_2 DECIMAL(10, 3) DEFAULT 0.000;
    IF (OLD.rrp_price <> NEW.rrp_price OR OLD.discount_1 <> NEW.discount_1 OR OLD.discount_2 <> NEW.discount_2 OR OLD.net_price <> NEW.net_price OR OLD.markup <> NEW.markup OR OLD.delivery_cost <> NEW.delivery_cost) THEN
        SET update_price = (SELECT b.is_auto_update FROM price_categories c INNER JOIN brands b ON b.brand_name = c.brand_name WHERE c.id = NEW.category_id LIMIT 1);
        IF (update_price = 1) THEN
            IF (NEW.is_bundle = 0) THEN
                UPDATE t2 SET temp = 'Fired Single' WHERE id = NEW.id;
            ELSE IF (NEW.is_bundle = 1) THEN
                UPDATE t2 SET temp = 'Fired Bundle' WHERE id = NEW.id;
            END IF;
        END IF;
    END IF;
END;
$$
DELIMITER ;

The current UPDATE statements are just placeholders for some actual calculations I'll end up doing.

Please note: I use Sequel Pro for most MySQL-related stuff and initially was using their GUI to try and add the trigger - it automatically adds the surrounding code so I would only create everything between the BEGIN and END statements. That also resulted in this same syntax error, so I don't believe it's related to the delimiters like some similar threads I've already found on here. Nevertheless, I've tried adding the full trigger code via a normal query; changing the delimiter syntax - for example END$$, END $$, END; $$ etc.

I've successfully created other triggers with similar syntax, but they do not include the DECLARE syntax.

Where am I going wrong?


Solution

  • The problem is here:

    IF (NEW.is_bundle = 0) THEN
        UPDATE t2 SET temp = 'Fired Single' WHERE id = NEW.id;
    ELSE IF (NEW.is_bundle = 1) THEN
        UPDATE t2 SET temp = 'Fired Bundle' WHERE id = NEW.id;
    END IF;
    

    Review documentation: https://dev.mysql.com/doc/refman/8.0/en/if.html

    MySQL supports ELSEIF and this is different than ELSE IF. If you use ELSEIF, this continues the structure of the IF statement. If you use ELSE IF, it starts a new IF statement, so it should be like this:

    IF (NEW.is_bundle = 0) THEN
        UPDATE t2 SET temp = 'Fired Single' WHERE id = NEW.id;
    ELSE 
        IF (NEW.is_bundle = 1) THEN
            UPDATE t2 SET temp = 'Fired Bundle' WHERE id = NEW.id;
        END IF;
    END IF;
    

    See that there is a complete IF/THEN/END IF statement within the ELSE block of the outer one?

    But you didn't do that, so the END IF applies to the innermost IF statement, and then you're one level off for the rest of the body of the trigger.

    When MySQL gets to the end of the whole CREATE TRIGGER statement, if there aren't enough ENDs to balance the blocks you began, MySQL complains with the error you saw.