CREATE PROCEDURE dbo.DeleteRateLevelChannel @ARateLevelChannelId INT AS
/*
  Created by Kent Johansen, Januarty 2021
  Changes:
  20230713: Added some tables
*/
DELETE RateLevelChannelCustomerXTab WHERE FKRateLevelChannelId = @ARateLevelChannelId

DELETE OTAAvailNotifLog WHERE FKRateLevelChannelId = @ARateLevelChannelId
DELETE OTAAvailNotifLogMulti WHERE FKRateLevelChannelId = @ARateLevelChannelId
DELETE OTAInvCountNotifLog WHERE FKRateLevelChannelId = @ARateLevelChannelId
DELETE RateLevelRestrictionRateChannelXTab WHERE FKRateLevelChannelId = @ARateLevelChannelId
DELETE RateLevelChannelRateLevelXTab WHERE FKRateLevelChannelId = @ARateLevelChannelId
DELETE RateLevelChannelCustomerXTab WHERE FKRateLevelChannelId = @ARateLevelChannelId
DELETE RateLevelChannel WHERE RateLevelChannelId = @ARateLevelChannelId

SELECT DISTINCT RateLevelId
INTO #tmpRateLevel
FROM RateLevel WITH (NOLOCK)
WHERE RateLevelId NOT IN (
  SELECT FKRateLevelId FROM RateLevelChannelRateLevelXTab WITH (NOLOCK)
)

DELETE RateLevelRoomTypeRestrictionXTab
WHERE FKRateLevelRoomTypeXTabId IN (
  SELECT Id
  FROM RateLevelRoomTypeXTab WITH (NOLOCK)
  WHERE FKRateLevelId IN (SELECT RateLevelId FROM #tmpRateLevel WITH (NOLOCK))
)

DELETE RateLevelRoomTypeRestrictionMulti
WHERE FKRateLevelRoomTypeXTabId IN (
  SELECT Id
  FROM RateLevelRoomTypeXTab WITH (NOLOCK)
  WHERE FKRateLevelId IN (SELECT RateLevelId FROM #tmpRateLevel WITH (NOLOCK))
)

DELETE RateLevelRoomTypeXTab WHERE FKRateLevelId IN (SELECT RateLevelId FROM #tmpRateLevel WITH (NOLOCK))
DELETE RateLevel WHERE RateLevelId IN (SELECT RateLevelId FROM #tmpRateLevel WITH (NOLOCK))

DROP TABLE #tmpRateLevel