Я попытался создать ваш сценарий на основе вашего описания.Это работает, но может привести вас к дополнительным вопросам относительно того, что я сделал и что вы пытаетесь сделать:
Настройка:
Create Table properties
(
property_id Int,
region_id Int,
property_name VarChar(25),
room_id Int
)
Insert Into properties Values
(1,1,'property1',1)
Create Table rooms
(
room_id Int,
rooms_occupancy_id Int, --(the combination of the occupancies associated with the room)
)
Insert Into rooms Values
(1,1)
Create Table rooms_occupancy --( stores the general details of the combination of the occupancies in a single room)
(
room_occupancy_id Int,
adult Int,
child Int,
infant Int
)
Insert Into rooms_occupancy Values
(1,1,2,0)
Create Table room_rate_types --(stores of the general details of the rates per room)
(
rate_id Int,
valid_from Date,
valid_to Date,
available_rooms Int,
rate_blk_date_id Int,
booking_id Int
)
Insert Into room_rate_types Values
(1,'2018-12-31','2019-01-01',6,1,1)
Create Table room_rate_type_blackout_dates --(stores the black out dates per rate)
(
rate_blk_date_id Int,
from_date Date,
to_date Date,
rate_id Int
)
Insert Into room_rate_type_blackout_dates Values
(1,'2018-12-29','2018-12-30',1)
Create Table room_rate_type_booked_dates --(stores the booking info of the rate table)
(
booking_id Int IDENTITY(1,1) NOT NULL,
from_date Date,
to_date Date,
rate_id Int,
booked_rooms Int
)
Процедура:
Declare @From As Date = '2018-12-31'
Declare @To As Date = '2019-01-01'
Declare @property As Int = 1
Declare @Region As Int = 1
Declare @roomratetypeid As Int = 1
If Exists
(
SELECT
p.property_id,
p.room_id,
rate_id = (Select rate_id From room_rate_types Where rate_id = @roomratetypeid),
Availablerooms = (Select available_rooms From room_rate_types Where rate_id = @roomratetypeid) - 1
FROM
properties p INNER JOIN
rooms r ON p.room_id = r.room_id INNER JOIN
rooms_occupancy ro ON r.rooms_occupancy_id = ro.room_occupancy_id
Where
p.property_id = @property And
p.region_id = @Region And
( --Check if requested dates are available
@From >= (Select valid_from From room_rate_types Where rate_id = @roomratetypeid) And
@To <= (Select valid_to From room_rate_types Where rate_id = @roomratetypeid)
) And
Not Exists (Select booking_id From room_rate_type_booked_dates Where @From >= from_date And @To <= to_date) And
Not Exists (Select rate_blk_date_id From room_rate_type_blackout_dates Where @From >= from_date And @To <= to_date)
) --If NOT exists, create booking and display results and adjust the available rooms
Begin
SELECT
p.property_id,
p.room_id,
rate_id = (Select rate_id From room_rate_types Where rate_id = @roomratetypeid),
Availablerooms = (Select available_rooms From room_rate_types Where rate_id = @roomratetypeid) - 1
FROM
properties p INNER JOIN
rooms r ON p.room_id = r.room_id INNER JOIN
rooms_occupancy ro ON r.rooms_occupancy_id = ro.room_occupancy_id
Where
p.property_id = @property And
p.region_id = @Region And
( --Check if requested dates are available
@From >= (Select valid_from From room_rate_types Where rate_id = @roomratetypeid) And
@To <= (Select valid_to From room_rate_types Where rate_id = @roomratetypeid)
)
Update room_rate_types Set available_rooms = (Select available_rooms From room_rate_types Where rate_id = @roomratetypeid) - 1;
Insert Into room_rate_type_booked_dates Values (@From,@To,@roomratetypeid,1)
End
Else
Select 'Not Available';
Результат:
property_id room_id rate_id Availablerooms
1 1 1 5