SET NOCOUNT ON
DECLARE @CustomerNumber INT
DECLARE @DiscountAll MONEY
DECLARE @DiscountRooms MONEY
DECLARE @DiscountHalls MONEY
DECLARE @DiscountMeeting MONEY
DECLARE @PhoneDiscount MONEY
DECLARE @PayTvDiscount MONEY
DECLARE @HotelNumber INT

DECLARE db_cur_cust CURSOR FOR
  SELECT CustomerNumber, DiscountAll, DiscountRooms, DiscountHalls, DiscountMeeting, PhoneDiscount, PayTvDiscount
  FROM Customer WITH(NOLOCK)
  WHERE (DiscountAll > 0
  OR DiscountRooms > 0
  OR DiscountHalls > 0
  OR DiscountMeeting > 0
  OR PhoneDiscount > 0
  OR PayTvDiscount > 0)
OPEN db_cur_cust

FETCH NEXT FROM db_cur_cust INTO @CustomerNumber, @DiscountAll, @DiscountRooms, @DiscountHalls, @DiscountMeeting, @PhoneDiscount, @PayTvDiscount

WHILE @@FETCH_STATUS = 0
BEGIN
  DECLARE db_cur_hot CURSOR FOR
    SELECT HotelNumber
    FROM Customer WITH (NOLOCK)
    WHERE CustomerTypeNumber = 7
    AND ISNULL(Customer.MarkAsDeleted, 0) = 0
    AND HotelNumber >= 0
  OPEN db_cur_hot  
  
  FETCH NEXT FROM db_cur_hot INTO @HotelNumber
  
  WHILE @@FETCH_STATUS = 0
  BEGIN
    UPDATE CustomerFieldsPerHotel SET DiscountAll = @DiscountAll, DiscountRooms = @DiscountRooms, DiscountHalls = @DiscountHalls,
      DiscountMetting = @DiscountMeeting, DiscountPhone = @PhoneDiscount, DiscountPayTv = @PayTvDiscount
    WHERE CustomerNumber = @CustomerNumber
    AND HotelNumber = @HotelNumber
    IF @@ROWCOUNT = 0
      INSERT INTO CustomerFieldsPerHotel (CustomerNumber, HotelNumber, DiscountAll, DiscountRooms, DiscountHalls, DiscountMetting, DiscountPhone, DiscountPayTv) VALUES 
      (@CustomerNumber, @HotelNumber, @DiscountAll, @DiscountRooms, @DiscountHalls, @DiscountMeeting, @PhoneDiscount, @PayTvDiscount)
    
    FETCH NEXT FROM db_cur_hot INTO @HotelNumber
  END 
  
  CLOSE db_cur_hot
  DEALLOCATE db_cur_hot   
    
  FETCH NEXT FROM db_cur_cust INTO @CustomerNumber, @DiscountAll, @DiscountRooms, @DiscountHalls, @DiscountMeeting, @PhoneDiscount, @PayTvDiscount
END

CLOSE db_cur_cust
DEALLOCATE db_cur_cust

EXEC sp_RENAME 'Customer.DiscountAll' , 'zzzDiscountAll', 'COLUMN'
EXEC sp_RENAME 'Customer.DiscountRooms' , 'zzzDiscountRooms', 'COLUMN'
EXEC sp_RENAME 'Customer.DiscountHalls' , 'zzzDiscountHalls', 'COLUMN'
EXEC sp_RENAME 'Customer.DiscountMeeting' , 'zzzDiscountMeeting', 'COLUMN'
EXEC sp_RENAME 'Customer.PhoneDiscount' , 'zzzPhoneDiscount', 'COLUMN'
EXEC sp_RENAME 'Customer.PayTvDiscount' , 'zzzPayTvDiscount', 'COLUMN'