1

我很难在我的 sql 查询中找到错误。

CREATE FUNCTION freeSeats(bookingID INT)
RETURNS VARCHAR(30)
BEGIN
DECLARE numberBooked INT ;
DECLARE status VARCHAR(30);

SELECT count(*) FROM passenger WHERE Booking IN(SELECT Id FROM booking WHERE
Flight = (SELECT Flight FROM booking WHERE Id = bookingID)) INTO numberBooked;

IF (numberBooked > 59) THEN SET status =”No free seats”;
ELSE SET status =”OK”;
END IF;

RETURN status;
END;

我收到此错误消息:

ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在“免费座位”附近使用的正确语法;其他设置状态 =“OK”;万一;

返回状态;END' 在第 10 行

我会很感激一些帮助。

提前致谢

4

1 回答 1

1
  • 不要为了得到改变DELIMITER
  • 使用单引号

例子,

DELIMITER $$
CREATE FUNCTION freeSeats(bookingID INT)
RETURNS VARCHAR(30)
BEGIN
    DECLARE numberBooked INT ;
    DECLARE status VARCHAR(30);

    SET numberBooked =
    (
    SELECT count(*) 
    FROM passenger 
    WHERE Booking IN 
        (   SELECT  Id 
            FROM    booking 
            WHERE   Flight = (SELECT Flight FROM booking WHERE Id = bookingID)
        ) 
    );

    IF (numberBooked > 59) THEN 
        SET status = 'No free seats';
    ELSE 
        SET status = 'OK';
    END IF;

    RETURN status;
END $$
DELIMITER ;
于 2013-01-12T00:48:49.937 回答