Advertisement
TonyTroev

18. Available Room

Feb 12th, 2019
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.03 KB | None | 0 0
  1. create function udf_GetAvailableRoom
  2.     (@HotelID int,
  3.     @Date date,
  4.     @People int)
  5. returns varchar(100)
  6. begin
  7.     declare @FreeRoomID int =
  8.         (select top 1
  9.             [R].Id
  10.         from
  11.             Hotels as [H]
  12.         join
  13.             Rooms as [R]
  14.         on
  15.             [H].Id = [R].HotelId
  16.         join
  17.             Trips as [T]
  18.         on
  19.             [R].Id = [T].RoomId
  20.         join
  21.             AccountsTrips as [AT]
  22.         on
  23.             [T].Id = [AT].TripId
  24.         where
  25.             [H].Id = @HotelID
  26.             and ([T].CancelDate is not null or @Date not between [T].ArrivalDate and [T].ReturnDate)
  27.             and [R].Beds >= @People
  28.         order by
  29.             [R].Price desc)
  30.  
  31.     if @FreeRoomID is null
  32.         return 'No rooms available'
  33.  
  34.     declare @RoomType varchar(50) = (select [Type] from Rooms where Id = @FreeRoomID)
  35.     declare @Beds int = (select Beds from Rooms where Id = @FreeRoomID)
  36.     declare @Price decimal(15, 2) = (select Price from Rooms where Id = @FreeRoomID)
  37.     declare @BaseRate decimal(15, 2) = (select BaseRate from Hotels where Id = @HotelID)
  38.  
  39.     return concat('Room ', @FreeRoomID, ': ', @RoomType, ' (', @Beds, ' beds) - $', (@BaseRate + @Price) * @People)
  40. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement