CREATE PROCEDURE dbo.DoDeleteHotelSetupSetting @AHotelSetupSearchKey VARCHAR(60) AS
SET NOCOUNT ON
/*
  Created by Kent Johansen april 2010
  Changes:
    KJ 20111207: Bug: Fix for "The DELETE statement conflicted with the REFERENCE constraint "FK_HotelSetupLog_LogPicasso""
  
*/

DECLARE @HotelSetupNumber INT
DECLARE @HotelSetupValueNumber INT
DECLARE @LogNumber INT

SELECT @HotelSetupNumber = HotelSetupNumber FROM HotelSetup WITH (NOLOCK) WHERE SearchKey = @AHotelSetupSearchKey

IF @HotelSetupNumber > 0
BEGIN
  DECLARE db_cur CURSOR FOR 
    SELECT FK_ValueNumber FROM HotelSetupValueXTab WITH (NOLOCK) WHERE HotelSetupNumber = @HotelSetupNumber
  OPEN db_cur
  
  FETCH NEXT FROM db_cur INTO @HotelSetupValueNumber
  WHILE @@FETCH_STATUS = 1
  BEGIN
    DELETE HotelSetupValue WHERE PK_ValueNumber = @HotelSetupValueNumber
    DELETE HotelSetupValueXTab WHERE FK_ValueNumber = @HotelSetupValueNumber
    
    FETCH NEXT FROM db_cur INTO @HotelSetupValueNumber
  END 
  
  SELECT @LogNumber = LogNumber FROM HotelSetupLog WITH (NOLOCK) WHERE HotelSetupNumber = @HotelSetupNumber
  
  DELETE HotelSetupLog WHERE HotelSetupNumber = @HotelSetupNumber
  DELETE HotelSetup WHERE HotelSetupNumber = @HotelSetupNumber
  DELETE LogPicasso WHERE LogNumber = @LogNumber
  
  CLOSE db_cur
  DEALLOCATE db_cur
END