创建触发器时出现“错误代码:2014。命令不同步;您现在不能运行此命令”

时间:2018-12-28 00:31:34

标签: mysql triggers

我正在尝试编写一个触发器,该触发器在插入的情况下会执行以下操作:

1)计算飞机上的座位数

2)计算已经在此航班上的乘客人数

3)比较这些值,然后在表中插入新行,或者引发错误,指出飞机上没有更多的自由座位了。

这是我的代码:

DELIMITER //
USE AIRPORT;
CREATE TRIGGER CHECK_FOR_PLACES
BEFORE INSERT ON TICKET
FOR EACH ROW
BEGIN
DECLARE NUM_OF_PLACES INT;
DECLARE NUM_OF_PASSENGERS INT;

SET NUM_OF_PLACES := (SELECT CAPACITY
FROM AIRPLANE
INNER JOIN FLIGHT ON FLIGHT.ID_AIRPLANE = AIRPLANE.ID_AIRPLANE
WHERE FLIGHT.ID_FLIGHT = NEW.ID_FLIGHT);

SET NUM_OF_PASSENGERS := (SELECT COUNT(*)
FROM TICKET
WHERE TICKET.ID_FLIGHT = NEW.ID_FLIGHT);

IF NUM_OF_PASSENGERS >= NUM_OF_PLACES THEN
    SIGNAL SQLSTATE '-20000' SET MESSAGE_TEXT = 'NO MORE PLACES ON THIS FLIGHT';
END IF;
END//
DELIMITER ; 

尝试编译脚本后,出现以下错误:

Error Code: 2014. Commands out of sync; you can't run this command now

我不知道此错误的原因可能是什么。我将不胜感激。预先感谢

1 个答案:

答案 0 :(得分:0)

use airport是一个单独的语句,由于您更改了定界符,因此应以//终止:

DELIMITER //
USE AIRPORT//
...