DECLARE @HotelSetupNumber INT
DECLARE @HotelCustomerNumber INT
DECLARE @HotelSetupValueNumber INT
DECLARE @KeyValue VARCHAR(2000)


SELECT @HotelSetupNumber = HotelSetUpNumber FROM HotelSetup (nolock) 
where SearchKey = 'AutoPrechargeEnable' and KeyValue = '1'

SET @HotelSetupNumber = ISNULL(@HotelSetupNumber, 0)

IF @HotelSetupNumber > 0
BEGIN
	IF NOT EXISTS (SELECT * FROM HotelSetupValueXTab (NOLOCK) WHERE HotelSetupNumber = @HotelSetupNumber)
	BEGIN
		DECLARE #cur CURSOR FOR
			select CustomerNumber from customer (NOLOCK)
			where CustomerTypeNumber = 7 and MarkAsDeleted = 0
		OPEN #cur
		FETCH NEXT FROM #cur INTO @HotelCustomerNumber
		WHILE @@FETCH_STATUS = 0
		BEGIN
			INSERT INTO HotelSetupValue(KeyValue)
			VALUES ('1')

			SELECT @HotelSetupValueNumber = @@IDENTITY

			INSERT INTO HotelSetupValueXTab (HotelSetupNumber, CustomerNumber, FK_ValueNumber)
			VALUES (@HotelSetupNumber, @HotelCustomerNumber, @HotelSetupValueNumber)

			
			SELECT @HotelSetupNumber = HotelSetUpNumber, @KeyValue = KeyValue FROM HotelSetup (nolock) 
			where SearchKey = 'AutoPrechargeReservationTypes' 

			IF ISNULL(@KeyValue, '') <> ''
			BEGIN
				INSERT INTO HotelSetupValue(KeyValue)
				VALUES (@KeyValue)

				SELECT @HotelSetupValueNumber = @@IDENTITY

				INSERT INTO HotelSetupValueXTab (HotelSetupNumber, CustomerNumber, FK_ValueNumber)
				VALUES (@HotelSetupNumber, @HotelCustomerNumber, @HotelSetupValueNumber)
			END

			SELECT @HotelSetupNumber = HotelSetUpNumber, @KeyValue = KeyValue FROM HotelSetup (nolock) 
			where SearchKey = 'AutoPrechargeReservationStatus' 

			IF ISNULL(@KeyValue, '') <> ''
			BEGIN
				INSERT INTO HotelSetupValue(KeyValue)
				VALUES (@KeyValue)

				SELECT @HotelSetupValueNumber = @@IDENTITY

				INSERT INTO HotelSetupValueXTab (HotelSetupNumber, CustomerNumber, FK_ValueNumber)
				VALUES (@HotelSetupNumber, @HotelCustomerNumber, @HotelSetupValueNumber)
			END

			FETCH NEXT FROM #cur INTO @HotelCustomerNumber
		END
		CLOSE #cur
		DEALLOCATE #cur
	END		
END