简体   繁体   English

这个sql触发器在哪里包含语法错误?

[英]Where is this sql trigger contain syntax error?

Spent about one hour to understand. 花了大约一个小时才明白。 Where is this sql trigger contain syntax error? 这个sql触发器在哪里包含语法错误?

CREATE
TRIGGER playlis_trubric_count_on_playlist_shared_update
AFTER UPDATE ON playlist_playlist
  FOR EACH ROW
    IF (NEW.shared != OLD.shared) AND (NEW.shared = 1) THEN
        UPDATE etv.playlist_playlistrubric 
            SET count = playlist_playlistrubric.count + 1
        WHERE etv.playlist_playlistrubric.id = NEW.rubric_id;
    ELSEIF (NEW.shared != OLD.shared) AND (NEW.shared = 0) THEN
        UPDATE etv.playlist_playlistrubric
            SET count = playlist_playlistrubric.count - 1
        WHERE etv.playlist_playlistrubric.id = NEW.rubric_id
    END IF;

ERROR SAYS: 错误说:

You have an error in your SQL syntax; 您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ELSEIF (NEW.shared != OLD.shared) AND (NEW.shared = 0) THEN UPDATE etv.p' at line 1 检查与MySQL服务器版本对应的手册,以便在'ELSEIF(NEW.shared!= OLD.shared)附近使用正确的语法AND(NEW.shared = 0)THEN UPDATE etv.p'在第1行

From the documentation : 文档

By using the BEGIN ... END construct, you can define a trigger that executes multiple statements. 通过使用BEGIN ... END构造,您可以定义执行多个语句的触发器。 Within the BEGIN block, you also can use other syntax that is permitted within stored routines such as conditionals and loops. BEGIN块中,您还可以使用存储例程(例如条件和循环)中允许的其他语法。 However, just as for stored routines, if you use the mysql program to define a trigger that executes multiple statements, it is necessary to redefine the mysql statement delimiter so that you can use the ; 但是,就像存储例程一样,如果使用mysql程序来定义执行多个语句的触发器,则需要重新定义mysql语句分隔符,以便可以使用; statement delimiter within the trigger definition. 触发器定义中的语句分隔符。 The following example illustrates these points. 以下示例说明了这些要点。 It defines an UPDATE trigger that checks the new value to be used for updating each row, and modifies the value to be within the range from 0 to 100. This must be a BEFORE trigger because the value needs to be checked before it is used to update the row: 它定义了一个UPDATE触发器,用于检查用于更新每一行的新值,并将该值修改为0到100之间的范围。这必须是BEFORE触发器,因为在将值用于之前需要检查该值更新行:

 mysql> delimiter // mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account -> FOR EACH ROW -> BEGIN -> IF NEW.amount < 0 THEN -> SET NEW.amount = 0; -> ELSEIF NEW.amount > 100 THEN -> SET NEW.amount = 100; -> END IF; -> END;// mysql> delimiter ; 

You're missing BEGIN and END and you're also going to need the delimiter trick, because at present your trigger definition ends at the first NEW.rubric_id (which is why the parse error occurs just after that point). 您缺少BEGINEND并且您还需要分隔符技巧,因为目前您的触发器定义在第一个NEW.rubric_id结束(这就是解析错误发生在该点之后的原因)。

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

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