创建触发器后,我收到以下错误:
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 3
这是我的查询:
CREATE TRIGGER reserved BEFORE INSERT ON Reserv
FOR EACH ROW BEGIN
DECLARE trip_id integer;
DECLARE free integer;
DECLARE count integer;
set @free := (select place_free from Trips where id = trip_id);
set @count := (select count from inserted);
if @count <= @free
begin
update Trips set place_free = @free - @count where id = @trip_id;
end
ELSE
BEGIN
ROLLBACK;
return;
END
END
最佳答案
我看到您正在使用 RETURN
语句。如https://dev.mysql.com/doc/refman/5.7/en/stored-program-restrictions.html#stored-routines-trigger-restrictions状态:
The RETURN statement is not permitted in triggers, which cannot return a value. To exit a trigger immediately, use the LEAVE statement.
但从我的角度来看,这是一个不同的主题,因为错误消息提到了第 3 行中的语法错误。也许您忘记将分隔符设置为与 ;
不同的内容:
DELIMITER //
CREATE TRIGGER ...
关于mysql - 如何在触发器中声明变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49536303/