简体   繁体   English

MySQL 创建触发器语法错误(最后一行)

[英]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.我正在创建一个 MySQL 触发器,如果某些值被 UPDATE 查询更改,则该触发器旨在用新值更新各种表。 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.在这种情况下,第 29 行是END语句的行。

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.当前的UPDATE语句只是我最终要做的一些实际计算的占位符。

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.请注意:我将 Sequel Pro 用于大多数与 MySQL 相关的东西,最初是使用他们的 GUI 来尝试添加触发器 - 它会自动添加周围的代码,所以我只会在BEGINEND语句之间创建所有内容。 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;更改分隔符语法 - 例如END$$END $$END; $$ etc. $$

I've successfully created other triggers with similar syntax, but they do not include the DECLARE syntax.我已经成功地创建了其他具有类似语法的触发器,但它们不包括DECLARE语法。

Where am I going wrong?我哪里错了?

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查看文档: https://dev.mysql.com/doc/refman/8.0/en/if.html

MySQL supports ELSEIF and this is different than ELSE IF . MySQL 支持ELSEIF ,这与ELSE IF不同。 If you use ELSEIF , this continues the structure of the IF statement.如果您使用ELSEIF ,这将延续IF语句的结构。 If you use ELSE IF , it starts a new IF statement, so it should be like this:如果你使用ELSE IF ,它会启动一个新的IF语句,所以它应该是这样的:

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?看到外部的ELSE块中有完整的IF/THEN/END IF语句吗?

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.但是您没有这样做,因此END IF适用于最内层的IF语句,然后您就比触发器主体的 rest 低一级。

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.当 MySQL 到达整个 CREATE TRIGGER 语句的末尾时,如果没有足够的 END 来平衡您开始的块,MySQL 会抱怨您看到的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM