触发器上的MySQL语法错误

时间:2014-07-01 08:07:13

标签: mysql triggers syntax-error

我无法在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;

当我运行时,我收到以下错误:

[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]

最初我已经嵌套了IF,但改为CASE,看看是否有任何区别。当前的形式不是我原来的形式(嵌套IF以避免重复相同的条件)但是每次尝试失败都会改变代码,但我尝试过的都没有用过。

列/表名称是正确的。

2 个答案:

答案 0 :(得分:1)

引发的错误是由于调用了MySQL date()函数。它被错误地使用了。

我不确定您的意图是否使用当前日期 如果是,您可以对代码中的所有date()用法使用以下任何建议。

  

curdate() - 返回当前日期
  current_date() - CURDATE()的同义词   current_date - CURDATE()

的同义词

示例

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

您也可以使用date()函数,但它需要datedatetimetimestamp类型的输入参数。

示例

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        |
+-------------------+---------------+-------------------+-------------------+

请参阅文档

答案 1 :(得分:0)

在开始之前用Delimiter $$更改分隔符,并使用新分隔符结束触发器。当你完成后再改回来,如下:

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