MySQL更新前触发

时间:2013-12-03 11:25:23

标签: mysql triggers

我想在mysql中添加触发器......

CREATE TRIGGER mytrigger BEFORE INSERT ON table_1
FOR EACH ROW
BEGIN SET 
NEW.`order` = (SELECT 1 + COALESCE((SELECT MAX(`order`) FROM `table_1`), 0));
END

我总是得到错误:

Error
SQL query: Documentation

CREATE TRIGGER mytrigger BEFORE INSERT ON table_1
FOR EACH
ROW 
BEGIN SET NEW.`order` = ( SELECT 1 + COALESCE( (


SELECT MAX(  `order` ) 
FROM  `table_1` ) , 0 )
);

MySQL said: Documentation

#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 4 

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您需要更改分隔符。否则引擎认为该语句在第一个;处完成。但这是错误的。

delimiter |
CREATE TRIGGER mytrigger BEFORE INSERT ON table_1
FOR EACH ROW
BEGIN 
  SET NEW.`order` = (SELECT 1 + COALESCE((SELECT MAX(`order`) FROM `table_1`), 0));
END
|
delimiter ;