简体   繁体   English

MySql 创建触发器以插入计算值时出现语法错误

[英]MySql Syntax error when creating trigger to insert calculated value

This is my first time trying to create a MySql trigger but I'm running into syntax errors that I can't identify.这是我第一次尝试创建 MySql 触发器,但我遇到了无法识别的语法错误。 I am trying to have the trigger insert a calculated value when a row is updated.我试图让触发器在更新行时插入计算值。 Below is my code, but I keep getting syntax errors when I try to execute it, and I cannot see where the error is.下面是我的代码,但是当我尝试执行它时,我不断收到语法错误,而且我看不到错误在哪里。 Can someone please look, what am I doing wrong?有人可以看看,我做错了什么?

DROP TRIGGER ins_cop_dhw;
CREATE TRIGGER ins_cop_dhw BEFORE INSERT ON `2017010001_data`
   FOR EACH ROW 
   BEGIN
       IF (NEW.`_007E` = 1 AND `_00A2` > 0) THEN
           SET NEW.`_00B7` = NEW.`_0096` / NEW.`_00A2`;
       END IF;
       IF (NEW.`_007D` = 1 AND `_00A2` > 0) THEN
           SET NEW.`_00B8` = (NEW.`_0096` / NEW.`_00A2`);
       END IF;
   END;

Here is the error I get in MySQL Workbench, but it's not much help.这是我在 MySQL Workbench 中遇到的错误,但这并没有多大帮助。 :/ :/

Error Code: 1064. 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 5

MySQL Workbench highlights the end of line 6 as the start of the syntax error. MySQL Workbench 将第 6 行的末尾突出显示为语法错误的开始。 MySql Workbench 语法错误高亮截图

As said by @Akina I was missing the delimiter statements.正如@Akina 所说,我缺少delimiter语句。 Here is fixed code.这是固定代码。

delimiter //
DROP TRIGGER ins_cop_dhw;
CREATE TRIGGER ins_cop_dhw BEFORE INSERT ON `2017010001_data`
   FOR EACH ROW 
   BEGIN
       IF (NEW.`_007E` = 1 AND NEW.`_00A2` > 0) THEN
           SET NEW.`_00B7` = NEW.`_0096` / NEW.`_00A2`;
       END IF;
       IF (NEW.`_007D` = 1 AND NEW.`_00A2` > 0) THEN
           SET NEW.`_00B8` = (NEW.`_0096` / NEW.`_00A2`);
       END IF;
   END//
   delimiter ;

As per the advice of @Akina I read the Create Procedure documentation from the dev.mysql.com site and the below quote highlighted the importance of using delimiter根据@Akina 的建议,我阅读了来自 dev.mysql.com 站点的 创建过程文档,下面的引用强调了使用delimiter的重要性

The example uses the mysql client delimiter command to change the statement delimiter from;该示例使用 mysql 客户端分隔符命令来更改语句分隔符; to // while the procedure is being defined. to // 在定义过程时。 This enables the;这使得; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself.过程主体中使用的分隔符将传递给服务器,而不是由 mysql 本身解释。 See Section 25.1, “Defining Stored Programs”.请参见第 25.1 节,“定义存储程序”。

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

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