drop procedure if exists AddValidBooking;
DELIMITER //
CREATE PROCEDURE AddValidBooking(IN booking_date DATE, IN table_number INT)
BEGIN
DECLARE booked BOOLEAN;
START TRANSACTION;
SELECT CASE WHEN EXISTS (
SELECT 1
FROM Bookings
WHERE DATE(Booking_Date) = DATE(booking_date) AND Table_Number = table_number
) THEN TRUE ELSE FALSE END INTO booked;
IF booked THEN
ROLLBACK;
SELECT CONCAT("table ", table_number, " is booked - booking canceled") AS booking_status;
ELSE
SELECT CONCAT("table ", table_number, " is free - booked successfully") AS booking_status;
INSERT INTO Bookings (Booking_Date, Table_Number) VALUES (booking_date, table_number);
COMMIT;
END IF;
END //
DELIMITER ;
CALL AddValidBooking('2023-11-7', 2);
虽然免费,但返回表 2 总是被预订 我尝试了多种日期与表号的组合,但它仍然返回相同的结果
您需要将查询更改为:
WHERE DATE(Bookings.Booking_Date) = DATE(booking_date) AND Bookings.Table_Number = table_number
或者更好,更改参数名称。