My question is:
Have an sql stored procedure which brings me a list of free rooms between two date in an hotel_reservation table for a given hotelid and bookin ,bookout dates.
Here is the procedure :
ALTER PROCEDURE dbo.StoredProcedure1
(
@DateStart date,
@DateEnd date,
@hotel_id int
)
AS
SELECT DISTINCT room_type_id, room_id
FROM Room
WHERE room_id NOT IN (
SELECT room_id
FROM Hotel_Reservation
WHERE @DateStart BETWEEN bookingStart AND bookingEnd
OR @DateEnd BETWEEN bookingStart AND bookingEnd
or bookingStart BETWEEN @DateStart AND @DateEnd
or bookingEnd BETWEEN @DateStart AND @DateEnd
and hotel_id = @hotel_id
) and hotel_id = @hotel_id
RETURN
Definition of Room table :
room_id int
hotel_id int
room_type_id varchar
price money
room_description varchar
In room table I used duplicated key with room_id and Hotel_id cause Hotel with id 201 can have room_id 101 also Hotel with id 202 can have room_id 101 to avoid from repeated columns I used this.
definition of hotel_reservation table
reservation_id int
customer_id varchar
bookingStart date
bookingEnd date
status varchar
room_id int
hotel_id int
date date
The Problem is:
When I run this procedure If a user booked a room with id 101 at hotel 201, another user cant see the room with id 101 at hotel with id 202. It books all the 101 id rooms from the hotels.
How can I improve my procedure to overcome this problem?
Thanks for viewing..
Change to NOT EXSTS which allows multiple conditions?
In this case it can check both hotel and room ids.
Edit:
Thank you to Andriy M for correcting my AND/OR operator precedence with some brackets