IF (SELECT COUNT (*)
    FROM INFORMATION_SCHEMA.COLUMNS WITH (NOLOCK) JOIN SYSCOLUMNS WITH (NOLOCK) ON 
	  object_id(INFORMATION_SCHEMA.COLUMNS.table_name) = SYSCOLUMNS.id 
             AND INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME = SYSCOLUMNS.NAME
             AND INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = 'CustomerFieldsPerHotel' 
             AND INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME = 'ProvisionNetto'
			 AND INFORMATION_SCHEMA.COLUMNS.DATA_TYPE = 'SMALLINT') = 1
BEGIN

  UPDATE CustomerFieldsPerHotel
  SET ProvisionNetto = 1
  WHERE ISNULL(ProvisionNetto, -1) > 1

  UPDATE CustomerFieldsPerHotel
  SET ProvisionNetto = 0
  WHERE ISNULL(ProvisionNetto, -1) < 0

  ALTER TABLE CustomerFieldsPerHotel ALTER COLUMN ProvisionNetto BIT NOT NULL 
  ALTER TABLE CustomerFieldsPerHotel ADD CONSTRAINT DF_CustomerFieldsPerHotel_ProvisionNetto DEFAULT 0 FOR ProvisionNetto

  DECLARE @CustomerNumber INTEGER
  DECLARE @ProvisionBill MONEY
  DECLARE @ProvisionAccount MONEY
  DECLARE @ProvisionRestaurant MONEY
  DECLARE @ProvisionPostOnGuest BIT
  DECLARE @ProvisionNetto BIT
  DECLARE @CountRecords INTEGER
  DECLARE @SELECT NVARCHAR(MAX)

  SET @CountRecords = 0

  SET @SELECT = 'DECLARE myCursor CURSOR FAST_FORWARD FOR
    SELECT 
      CustomerNumber,
	  ISNULL(ProvisionBill, 0) ProvisionBill,
	  ISNULL(ProvisionAccount, 0) ProvisionAccount,
	  ISNULL(Restaurant, 0) ProvisionRestaurant,
	  ProvisionPostOnGuest,
	  CAST (NetCash AS BIT) ProvisionNetto
    FROM CUSTOMER (NOLOCK)
    WHERE 
	  CustomerTypeNumber IN (2, 3, 6) AND 
      ISNULL(ProvisionBill, 0) + 
	  ISNULL(ProvisionAccount, 0) + 
	  ISNULL(Restaurant, 0) +
 	  ISNULL(ProvisionPostOnGuest, 0) +
	  ISNULL(NetCash, 0) > 0 '

  EXEC (@SELECT)	  
  OPEN myCursor 
  FETCH NEXT FROM myCursor 
  INTO 
    @CustomerNumber,
	@ProvisionBill, 
	@ProvisionAccount, 
	@ProvisionRestaurant, 
	@ProvisionPostOnGuest, 
	@ProvisionNetto

  WHILE @@FETCH_STATUS > -1
  BEGIN
    UPDATE CUSTOMERFIELDSPERHOTEL
    SET 
	  ProvisionBill = @ProvisionBill,
	  ProvisionAccount = @ProvisionAccount,
	  ProvisionRestaurant = @ProvisionRestaurant,
	  ProvisionPostOnGuest = @ProvisionPostOnGuest,
	  ProvisionNetto = @ProvisionNetto
	WHERE CustomerNumber = @CustomerNumber

    SET @CountRecords = @CountRecords + 1  

    FETCH NEXT FROM myCursor 
    INTO 
      @CustomerNumber,
	  @ProvisionBill,
	  @ProvisionAccount,
	  @ProvisionRestaurant,
	  @ProvisionPostOnGuest,
	  @ProvisionNetto
  END

  CLOSE myCursor
  DEALLOCATE myCursor

  --PRINT @CountRecords

END






