IF EXISTS (select * from HotelSetup hs (nolock)
	where hs.SearchKey IN ('DefaultTemplateCreation', 'DefaultTemplateCheckin', 'DefaultTemplateCheckinNotPaid','DefaultTemplateCheckoutNotPaid', 'DefaultTemplateCreateNotPaid'))
BEGIN
	DECLARE @KeyValue varchar(2000),
	@TemplateNumber INT,
	@SearchKey VARCHAR(2000)

	DECLARE #cur CURSOR FOR
		select hs.SearchKey,hv.KeyValue from HotelSetup hs (nolock)
		inner join HotelSetupValueXTab hvx (nolock) on hs.HotelSetUpNumber = hvx.HotelSetupNumber
		inner join HotelSetupValue hv (nolock) on hvx.FK_ValueNumber = hv.PK_ValueNumber
		where hs.SearchKey IN ('DefaultTemplateCreation', 'DefaultTemplateCheckin', 'DefaultTemplateCheckinNotPaid','DefaultTemplateCheckoutNotPaid', 'DefaultTemplateCreateNotPaid')

	OPEN #cur

	FETCH NEXT FROM #cur INTO @SearchKey, @KeyValue

	WHILE @@FETCH_STATUS = 0
	BEGIN
		SET @TemplateNumber = CAST(@KeyValue AS INT)
		UPDATE Templates
		SET Type_ = CASE @SearchKey 
						WHEN 'DefaultTemplateCreation' THEN 1
						WHEN 'DefaultTemplateCheckin' THEN 2
						WHEN 'DefaultTemplateCheckinNotPaid' THEN 3
						WHEN 'DefaultTemplateCheckoutNotPaid' THEN 4
						WHEN 'DefaultTemplateCreateNotPaid' THEN 5
						ELSE 0
					END
		WHERE TemplateNumber = @TemplateNumber
		FETCH NEXT FROM #cur INTO @SearchKey, @KeyValue
	END

	Close #cur 
	DEALLOCATE #cur

	DELETE FROM HotelSetupValueXTAB
	FROM HotelSetupValueXTab hvx  
	INNER JOIN HotelSetup hs on hs.HotelSetUpNumber = hvx.HotelSetupNumber
	where hs.SearchKey IN ('DefaultTemplateCreation', 'DefaultTemplateCheckin', 'DefaultTemplateCheckinNotPaid','DefaultTemplateCheckoutNotPaid', 'DefaultTemplateCreateNotPaid')

	DELETE FROM HotelSetupValue
	FROM HotelSetupValue hv
	inner join HotelSetupValueXTab hvx  on hvx.FK_ValueNumber = hv.PK_ValueNumber
	INNER JOIN HotelSetup hs on hs.HotelSetUpNumber = hvx.HotelSetupNumber
	where hs.SearchKey IN ('DefaultTemplateCreation', 'DefaultTemplateCheckin', 'DefaultTemplateCheckinNotPaid','DefaultTemplateCheckoutNotPaid', 'DefaultTemplateCreateNotPaid')

	DELETE FROM HotelSetupLog 
	FROM HotelSetupLog hsl 
	INNER JOIN HotelSetup hs On Hs.HotelSetUpNumber = hsl.HotelSetupNumber
	where hs.SearchKey IN ('DefaultTemplateCreation', 'DefaultTemplateCheckin', 'DefaultTemplateCheckinNotPaid','DefaultTemplateCheckoutNotPaid', 'DefaultTemplateCreateNotPaid')

	DELETE FROM HotelSetup
	where SearchKey IN ('DefaultTemplateCreation', 'DefaultTemplateCheckin', 'DefaultTemplateCheckinNotPaid','DefaultTemplateCheckoutNotPaid', 'DefaultTemplateCreateNotPaid')

END