简体   繁体   English

触发器上的MySQL语法错误

[英]MySQL Syntax Error on Trigger

I cannot get this trigger code to compile on MySQL 5.3: 我无法在MySQL 5.3上编译此触发代码:

CREATE TRIGGER crmenq_bur 
BEFORE UPDATE 
ON crmenquiries
FOR EACH ROW
BEGIN
   IF (    NEW.feedback = OLD.feedback 
       AND NEW.notes = OLD.notes
       AND NEW.estatus = OLD.estatus )
   THEN
      SET NEW.update_type = 'NN';
   ELSE
      SET NEW.update_type = 'SEN';
      SET new.last_update2 = now();
   END IF;
   CASE 
      WHEN NEW.estatus != OLD.estatus AND NEW.estatus = 'FP3' THEN
         SET NEW.uid_follow_up = NEW.uid_last_update_by;
         SET NEW.follow_up_date = DATE_ADD ( date(), INTERVAL 3 MONTH );
      WHEN NEW.estatus != OLD.estatus AND NEW.estatus = 'FP6' THEN
         SET NEW.uid_follow_up = NEW.uid_last_update_by;
         SET NEW.follow_up_date = DATE_ADD ( date(), INTERVAL 6 MONTH );
      WHEN NEW.estatus != OLD.estatus AND NEW.estatus = 'FP9' THEN
         SET NEW.uid_follow_up = NEW.uid_last_update_by;
         SET NEW.follow_up_date = DATE_ADD ( date(), INTERVAL 9 MONTH );
      WHEN NEW.estatus != OLD.estatus AND NEW.estatus = 'FP12' THEN
         SET NEW.uid_follow_up = NEW.uid_last_update_by;
         SET NEW.follow_up_date = DATE_ADD ( date(), INTERVAL 12 MONTH );
      WHEN NEW.estatus NOT IN ( 'FP3', 'FP6', 'FP9', 'FP12' ) THEN
         SET NEW.uid_follow_up = NULL;
         SET NEW.follow_up_date = NULL;
   END CASE;
END;

When I run this I get the following error: 运行此命令时,出现以下错误:

[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 '), INTERVAL 3 MONTH ); WHEN NEW.estatus != OLD.estatus AND NEW.estatus = ' at line 18]

Initially I had nested IF but changed to CASE to see if that made any difference. 最初,我嵌套了IF,但改用CASE看看是否有什么不同。 The current form is not how I had it originally (nested IF to avoid repeating the same condition) but with each unsuccessful attempt the code was changed but nothing I have tried has worked. 当前的格式不是我最初使用的格式(如果避免重复相同的条件,则嵌套该格式),但是每次尝试均失败,则代码被更改,但我尝试过的任何方法都没有起作用。

The column/tables names are correct. 列/表名称正确。

Error thrown is due to call on MySQL date() function. 引发错误是由于调用MySQL date()函数引起的。 It was wrongly used. 它被错误地使用了。

I am not sure if your intention was to use current date. 我不确定您是否打算使用当前日期。
If yes, you can use any of the following suggestions on all date() usages in your code. 如果是,则可以对代码中的所有date()用法使用以下任何建议。

curdate() -- Return the current date curdate() -返回当前日期
current_date() -- Synonyms for CURDATE() current_date() - CURDATE()同义词
current_date -- Synonyms for CURDATE() current_date - CURDATE()同义词

Example : 范例

mysql> select curdate(), current_date(), current_date;
+------------+----------------+--------------+
| curdate()  | current_date() | current_date |
+------------+----------------+--------------+
| 2014-07-01 | 2014-07-01     | 2014-07-01   |
+------------+----------------+--------------+

You can also use date() function but it takes an input parameter of type date , datetime or timestamp . 您也可以使用date()函数,但是它使用类型为datedatetimetimestamp的输入参数。

Example : 范例

mysql> select date( sysdate() ), date( now() ), date( curtime() ), date( curdate() );
+-------------------+---------------+-------------------+-------------------+
| date( sysdate() ) | date( now() ) | date( curtime() ) | date( curdate() ) |
+-------------------+---------------+-------------------+-------------------+
| 2014-07-01        | 2014-07-01    | 2014-07-01        | 2014-07-01        |
+-------------------+---------------+-------------------+-------------------+

Refer to documentation : 请参阅文档

Change the delimiter with something like Delimiter $$ before you start, and end your trigger with the new delimiter. 在开始之前,使用Delimiter $$类的内容更改定界符,并使用新的定界符结束触发器。 Change it back when you're done, like this: 完成后将其改回,如下所示:

Delimiter $$
CREATE TRIGGER crmenq_bur 
BEFORE UPDATE 
ON crmenquiries
FOR EACH ROW
BEGIN
 /* Body of code here */
END $$
Delimiter ;

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

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