MySQL触发器创建问题

时间:2015-03-31 17:24:52

标签: mysql triggers

尝试创建触发器时出现此错误 - #1064 - 您的SQL语法出错;检查与您的MySQL服务器版本对应的手册,以便在DECLARE orderCount INT附近使用正确的语法;  SET orderCount =(SELECT COUNT(Order_No)FROM`s order' at line 5

delimiter /

CREATE TRIGGER CountOrders

BEFORE INSERT ON order 

DECLARE orderCount INT;

SET orderCount = (SELECT COUNT(Order_No) FROM `order` WHERE Outlet = 1 AND
complete ='FALSE');       
BEGIN

IF orderCount > 3
THEN SIGNAL 'To many orders pending'

END;
/

delimiter ;

我们非常感谢任何建议。谢谢。

1 个答案:

答案 0 :(得分:0)

触发器中有几个问题。这是一个修改。 declare部分应该在begin之后,并且order是保留字,因此需要包含反引号

delimiter //

CREATE TRIGGER CountOrders BEFORE INSERT ON `order` 
for each row
begin
 DECLARE orderCount INT;
 SELECT COUNT(Order_No) into orderCount FROM `order` 
 WHERE Outlet = 1 AND complete ='FALSE' ; 
 IF orderCount > 3 then
   SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'To many orders pending';
 end if;
end ;//

delimiter ;

https://dev.mysql.com/doc/refman/5.5/en/signal.html