CREATE PROCEDURE [dbo].[CopyCustomerProfile] 
  @CustomerTypeNumber SMALLINT,
  @CustomerNumberTo INTEGER,
  @CustomerNumberFrom INTEGER,
  @HotelNumber INTEGER,
  @CustomerProfile CHAR(18), 
  @CopyDownTo CHAR(6), 
  @UserID VARCHAR(10), 
  @Date CHAR(8), 
  @FullName VARCHAR(50), 
  @NumberOfLinks INTEGER OUTPUT,
  @NumberOfLocks INTEGER OUTPUT AS
SET NOCOUNT ON

/*
Changes:
  VD 20150102: EXT: Handling customer allotments being moved from CustomerFieldsPerHotel to CustomerAllotmentsXTab  
  VD 20141212: BUG: Handling error. Copying Provisions and Discounts on their own didn't work. Some housekeeping.
  KJ 20131021: Discount per hotel 
  VD 20130901: EXT: Copying mailaddresses adjusted to the new situation where mail addresses are placed in separate table.
  VD 20120613: CLN: Adjustments i profiling Notes.
  BA 20101202: BUG: 'from Customer (NoWait,ReadPast,ROWLOCK)' was missing the WITH KeyWord.
  VD 20100301: Bug: Sp CopyAttachments replaced by sp CopyCustomerAttachments. 
  KJ 20091209: Ext: Removed Debtors.
  OF 20090316: Ext: PaymentAfterMonth and PaymentAfterDays added.
  OF 20090211: Bug: Some scroll_locks changed made. Did not work proberly.
  OF 20090206: EXT: Stored procedure CopyAttachments added.
  OF 20090112: Bug: Select statements uncommented and Rowlock bug fixed.
*/

Create Table #FoundValues (
  CustomerNumber int NOT NULL,
  CustomerTypeNumber Smallint NOT NULL)

Create Table #ResultTAB (
  CustomerNumber int NOT NULL,
  CustomerTypeNumber Smallint NOT NULL)

/* Fields from Customer */
Declare @CustomerNumberInTab int
Declare @CustomerTypeNumberInTab SmallInt
Declare @PaymentType Int
Declare @PaymentAfterMonth Int
Declare @PaymentAfterDays Int
Declare @CreditMax Money
Declare @Currency Int

Declare @ProvisionBill Money
Declare @ProvisionAccount Money
Declare @ProvisionNetCash SmallInt
Declare @ProvisionRestaurant Money

Declare @DiscountAll Money
Declare @DiscountRooms Money
Declare @DiscountHalls Money
Declare @DiscountMeeting Money
Declare @DiscountPhone Money
DECLARE @DiscountPayTv MONEY
Declare @MarketSegment Int
Declare @POS int
Declare @Purpose int
Declare @GuestType int
Declare @IATANumber VarChar(30)
-- Declare @EmailAdd Varchar(50)
Declare @WWWAdd VarChar(100)
Declare @CompanyNumber VarChar(30)
Declare @FlagA VarChar(50)
Declare @FlagB VarChar(50)
Declare @FlagC VarChar(50)
Declare @FlagD VarChar(50)
Declare @SplitBill Int

/* Fields from CustomerFieldsPerHotel */
Declare @CFCustomerNumber int
Declare @CFHotelNumber int
--Declare @CFAllotment1 Int
--Declare @CFAllotment2 Int
--Declare @CFAllotment3 Int
--Declare @CFAllotment4 Int
Declare @CFOvernightSingleProduct Int
Declare @CFOvernightGroupProduct Int
Declare @CFOvernightConferenceProduct Int
Declare @CFOvernightWalkInProduct Int
Declare @CFOvernightDaymeetingProduct Int
Declare @CFOvernightInternetProduct Int
Declare @CFOvernightOtherProduct1 Int
Declare @CFOvernightOtherProduct2 Int
Declare @CFNextOvernightSingleProduct Int
Declare @CFNextOvernightGroupProduct Int
Declare @CFNextOvernightConferenceProduct Int
Declare @CFNextOvernightWalkInProduct Int
Declare @CFNextOvernightDaymeetingProduct Int
Declare @CFNextOvernightInternetProduct Int
Declare @CFNextOvernightOtherProduct1 Int
Declare @CFNextOvernightOtherProduct2 Int
Declare @CFDaySingleProduct Int
Declare @CFDayGroupProduct Int
Declare @CFDayConferenceProduct Int
Declare @CFDayWalkInProduct Int
Declare @CFDayDaymeetingProduct Int
Declare @CFDayInternetProduct Int
Declare @CFDayOtherProduct1 Int
Declare @CFDayOtherProduct2 Int
Declare @CFNextDaySingleProduct Int
Declare @CFNextDayGroupProduct Int
Declare @CFNextDayConferenceProduct Int
Declare @CFNextDayWalkInProduct Int
Declare @CFNextDayDaymeetingProduct Int
Declare @CFNextDayInternetProduct Int
Declare @CFNextDayOtherProduct1 Int
Declare @CFNextDayOtherProduct2 Int
Declare @CFDateSingleProductChange datetime
Declare @CFDateGroupProductChange datetime
Declare @CFDateConferenceProductChange datetime
Declare @CFDateWalkInProductChange datetime
Declare @CFDateDaymeetingProductChange datetime
Declare @CFDateInternetProductChange datetime
Declare @CFDateOtherProduct1Change datetime
Declare @CFDateOtherProduct2Change datetime

/*-----------------------------------------*/

Declare @CustomerNumberSelectedTo Int
Declare @CustomerTypeNumberSelectedTo SmallInt

Declare @PaymentTypeTo Int
Declare @PaymentAfterMonthTo Int
Declare @PaymentAfterDaysTo Int
Declare @CreditMaxTo Money
Declare @CurrencyTo Int
Declare @ProvisionBillTo Money
Declare @ProvisionAccountTo Money
Declare @ProvisionNetCashTo SmallInt
Declare @ProvisionRestaurantTo Money
Declare @MarketSegmentTo Int
Declare @POSTo int
Declare @PurposeTo int
Declare @GuestTypeTo int
Declare @IATANumberTo VarChar(30)
-- Declare @EmailAddTo Varchar(50)
Declare @WWWAddTo VarChar(100)
Declare @CompanyNumberTo VarChar(30)
Declare @FlagATo VarChar(50)
Declare @FlagBTo VarChar(50)
Declare @FlagCTo VarChar(50)
Declare @FlagDTo VarChar(50)
Declare @SplitBillTo Int

/*-----------------------------------------*/

Declare @StrCustomerNumber char(6)
Declare @TransActionName VarChar(7)

If  @CustomerTypeNumber=2 --Company
begin
  if (SubString(@CopyDownTo,1,1)='1')
    Exec FindValues  @CustomerNumberTo,1
end
else
If  @CustomerTypeNumber=3  -- Agency
begin
  if (SubString(@CopyDownTo,1,1)='1')
     Exec FindValues  @CustomerNumberTo,1
  if (SubString(@CopyDownTo,2,1)='1')
  begin
      Exec FindValues  @CustomerNumberTo,2
      Exec  AllDownLinksCompany
  end
  if (SubString(@CopyDownTo,6,1)='1')
     Exec FindValues  @CustomerNumberTo,6
end
else
If  @CustomerTypeNumber=4 -- CompanyCorp
begin
  if (SubString(@CopyDownTo,2,1)='1')
  begin
      Exec FindValues  @CustomerNumberTo,2
      Exec  AllDownLinksCompany
  end
  if (SubString(@CopyDownTo,6,1)='1')
     Exec FindValues  @CustomerNumberTo,6
end
else
If  @CustomerTypeNumber=5  -- AgencyCorp
begin
  Exec FindValues  @CustomerNumberTo,3
  Exec  AllDownLinksAgency
end
else
If  @CustomerTypeNumber=7  -- SalesChannel
begin
  if (SubString(@CopyDownTo,1,1)='1')
     Exec FindValues  @CustomerNumberTo,1
  
  if (SubString(@CopyDownTo,2,1)='1')
  begin
      Exec FindValues  @CustomerNumberTo,2
      Exec  AllDownLinksCompany
  end
  if (SubString(@CopyDownTo,3,1)='1')
  begin
      Exec FindValues  @CustomerNumberTo,3
      Exec  AllDownLinksAgency
  end
  if (SubString(@CopyDownTo,4,1)='1')
  begin
      Exec FindValues  @CustomerNumberTo,4
      Exec  AllDownLinksCompanyCorp
  end
  if (SubString(@CopyDownTo,5,1)='1')
  begin
      Exec FindValues  @CustomerNumberTo,5
      Exec  AllDownLinksAgencyCorp
  end
  if (SubString(@CopyDownTo,6,1)='1')
    Exec FindValues  @CustomerNumberTo,6
end;

Insert Into #ResultTAB(CustomerNumber,CustomerTypeNumber) Values(@CustomerNumberTo,@CustomerTypeNumber) -- Der er kun en !!! (Øverste i linktræet)

Declare CopyCur Cursor Forward_Only FOR
Select CustomerNumber,CustomerTypeNumber from #FoundValues
Open CopyCur
Fetch Next From CopyCur
into @CustomerNumberInTab,@CustomerTypeNumberInTab

While(@@Fetch_status=0)
Begin
  if not Exists (Select CustomerNumber from #ResultTAB WITH (NOLOCK) where CustomerNumber=@CustomerNumberInTab)
  begin
--    print(@CustomerNumberInTab)
    Insert Into #ResultTAB(CustomerNumber,CustomerTypeNumber) Values(@CustomerNumberInTab,@CustomerTypeNumberInTab)
  end
  Fetch Next From CopyCur into @CustomerNumberInTab,@CustomerTypeNumberInTab
end
Close CopyCur
DeAllocate CopyCur

DECLARE @CopyAllotments BIT
DECLARE @CopyProduct BIT
DECLARE @CopyNextProduct BIT
DECLARE @CopyDiscounts BIT

--IF ((SUBSTRING(@CustomerProfile, 1, 1) = '1') AND (@CustomerTypeNumber < 7))
IF SUBSTRING(@CustomerProfile, 1, 1) = '1'
  SET @CopyAllotments = 1
ELSE
  SET @CopyAllotments = 0

--IF ((SUBSTRING(@CustomerProfile, 2, 1) = '1') AND (@CustomerTypeNumber < 7))
IF SUBSTRING(@CustomerProfile, 2, 1) = '1'
  SET @CopyProduct = 1
ELSE
  SET @CopyProduct = 0

--IF ((SUBSTRING(@CustomerProfile, 3, 1) = '1') AND (@CustomerTypeNumber < 7))
IF SUBSTRING(@CustomerProfile, 3, 1) = '1'
  SET @CopyNextProduct = 1
ELSE
  SET @CopyNextProduct = 0

--IF ((SUBSTRING(@CustomerProfile, 5, 1) = '1') AND (@CustomerTypeNumber < 7))
IF SUBSTRING(@CustomerProfile, 5, 1) = '1'
  SET @CopyDiscounts = 1
ELSE
  SET @CopyDiscounts = 0

IF @CopyAllotments = 1 
  EXEC CopyAllotments @CustomerNumberFrom, @CustomerNumberTo, @HotelNumber

SELECT 
  @PaymentType = PaymentTypeWareNumber, @PaymentAfterMonth = PaymentAfterMonth, 
  @PaymentAfterDays = PaymentAfterDays, @CreditMax = CreditMax, @Currency = CurrencyNumber,
  @ProvisionBill = ProvisionBill, @ProvisionAccount = ProvisionAccount,
  @ProvisionNetCash = NetCash, @ProvisionRestaurant = Restaurant, 
  @MarketSegment = SegmentNumber, @POS = PointOfSaleNumber, @Purpose = PurposeCodeNumber, @GuestType = GuestTypeNumber,
  @IATANumber = IATANumber, -- @EmailAdd = Email,
  @WWWAdd = WWWAddress, @CompanyNumber = CompanyNumber, 
  @FlagA = FlagA, @FlagB = FlagB, @FlagC = FlagC, @FlagD = FlagD,
  @SplitBill = SplitBillNumber
FROM Customer WITH (NOLOCK) WHERE CustomerNumber=@CustomerNumberFrom

Declare CustomerFieldsPerHotelCur Cursor SCROLL FOR
Select CustomerNumber, HotelNumber, 
	OvernightProductNumber1, OvernightProductNumber2, OvernightProductNumber3, OvernightProductNumber4,
	OvernightProductNumber5, OvernightProductNumber6, OvernightProductNumber7, OvernightProductNumber8, 
	NextOvernightProductNumber1, NextOvernightProductNumber2, NextOvernightProductNumber3, NextOvernightProductNumber4, 
	NextOvernightProductNumber5, NextOvernightProductNumber6, NextOvernightProductNumber7, NextOvernightProductNumber8,	
	DayProductNumber1, DayProductNumber2, DayProductNumber3, DayProductNumber4, 
	DayProductNumber5, DayProductNumber6, DayProductNumber7, DayProductNumber8,
	NextDayProductNumber1, NextDayProductNumber2, NextDayProductNumber3, NextDayProductNumber4, 
	NextDayProductNumber5, NextDayProductNumber6, NextDayProductNumber7, NextDayProductNumber8, 
	--AllotmentNumber1, AllotmentNumber2, AllotmentNumber3, AllotmentNumber4,
	ProductDate1, ProductDate2, ProductDate3, ProductDate4, ProductDate5, ProductDate6, ProductDate7, ProductDate8,
	DiscountAll, DiscountRooms, DiscountHalls, DiscountMetting, DiscountPhone, DiscountPayTv
from CustomerFieldsPerHotel WITH (nolock) where CustomerNumber=@CustomerNumberFrom
Open CustomerFieldsPerHotelCur

Set @NumberOfLinks = 0
Set @NumberOfLocks = 0

--Print  @UserID

SET TRANSACTION ISOLATION LEVEL READ COMMITTED  -- Important for SQL Server 2000

Declare CopyCurTo Cursor scroll_locks FOR
Select
    CustomerNumber, CustomerTypeNumber, PaymentTypeWareNumber, PaymentAfterMonth, 
    PaymentAfterDays, CreditMax, CurrencyNumber, ProvisionBill, ProvisionAccount, 
    NetCash, Restaurant, 
    SegmentNumber, PointOfSaleNumber, PurposeCodeNumber, GuestTypeNumber, IATANumber, -- Email, 
    WWWAddress, CompanyNumber, FlagA, FlagB, FlagC, FlagD, SplitBillNumber
from Customer WITH (NoWait,ReadPast,ROWLOCK) 
where CustomerNumber in (Select CustomerNumber from #ResultTAB WITH (NOLOCK))
Open CopyCurTo
FETCH NEXT FROM CopyCurTo
INTO  
  @CustomerNumberSelectedTo, @CustomerTypeNumberSelectedTo, @PaymentTypeTo, @PaymentAfterMonthTo, 
  @PaymentAfterDaysTo, @CreditMaxTo, @CurrencyTo,@ProvisionBillTo, @ProvisionAccountTo, 
  @ProvisionNetCashTo, @ProvisionRestaurantTo, 
  @MarketSegmentTo, @POSTo, @PurposeTo, @GuestTypeTo, @IATANumberTo, -- @EmailAddTo, 
  @WWWAddTo, @CompanyNumberTo, @FlagATo, @FlagBTo, @FlagCTo, @FlagDTo, @SplitBillTo

SET @NumberOfLinks = 0

WHILE(@@Fetch_status<>-1)
BEGIN

  SET @NumberOfLinks = @NumberOfLinks + 1
  SET @StrCustomerNumber = '000000'
  SET @StrCustomerNumber = STUFF(@StrCustomerNumber,(LEN(@StrCustomerNumber)+1)-LEN(LTRIM(Str(@CustomerNumberFrom))),LEN(LTRIM(Str(@CustomerNumberFrom))),LTRIM(Str(@CustomerNumberFrom)))

  --IF @CopyAllotments = 1 OR @CopyProduct = 1 OR @CopyNextProduct = 1 OR @CopyDiscounts = 1
  IF @CopyProduct = 1 OR @CopyNextProduct = 1 OR @CopyDiscounts = 1
  BEGIN

    FETCH FIRST FROM CustomerFieldsPerHotelCur 
    INTO @CFCustomerNumber, @CFHotelNumber, 
         @CFOvernightSingleProduct, @CFOvernightGroupProduct, @CFOvernightConferenceProduct, @CFOvernightWalkInProduct,
         @CFOvernightDaymeetingProduct, @CFOvernightInternetProduct, @CFOvernightOtherProduct1, @CFOvernightOtherProduct2,
         @CFNextOvernightSingleProduct, @CFNextOvernightGroupProduct, @CFNextOvernightConferenceProduct, @CFNextOvernightWalkInProduct,
         @CFNextOvernightDaymeetingProduct, @CFNextOvernightInternetProduct, @CFNextOvernightOtherProduct1, @CFNextOvernightOtherProduct2,
         @CFDaySingleProduct, @CFDayGroupProduct, @CFDayConferenceProduct, @CFDayWalkInProduct, 
         @CFDayDaymeetingProduct, @CFDayInternetProduct, @CFDayOtherProduct1, @CFDayOtherProduct2,
         @CFNextDaySingleProduct, @CFNextDayGroupProduct, @CFNextDayConferenceProduct, @CFNextDayWalkInProduct,
         @CFNextDayDaymeetingProduct, @CFNextDayInternetProduct, @CFNextDayOtherProduct1, @CFNextDayOtherProduct2,
         --@CFAllotment1, @CFAllotment2, @CFAllotment3, @CFAllotment4,
         @CFDateSingleProductChange, @CFDateGroupProductChange, @CFDateConferenceProductChange, @CFDateWalkInProductChange,
         @CFDateDaymeetingProductChange, @CFDateInternetProductChange, @CFDateOtherProduct1Change, @CFDateOtherProduct2Change,
         @DiscountAll, @DiscountRooms, @DiscountHalls, @DiscountMeeting, @DiscountPhone, @DiscountPayTv

    WHILE (@@Fetch_status<>-1)
    BEGIN
      IF NOT EXISTS (SELECT CustomerNumber 
                     FROM CustomerFieldsPerHotel WITH (NOLOCK) 
                     WHERE 
                       CustomerNumber = @CustomerNumberSelectedTo AND 
                       HotelNumber = @CFHotelNumber)
        INSERT INTO CustomerFieldsPerHotel (CustomerNumber, HotelNumber) VALUES (@CustomerNumberSelectedTo, @CFHotelNumber)
/*
      IF @CopyAllotments=1
        UPDATE CustomerFieldsPerHotel 
        SET AllotmentNumber1=@CFAllotment1, AllotmentNumber2=@CFAllotment2, AllotmentNumber3=@CFAllotment3, AllotmentNumber4=@CFAllotment4
        WHERE 
          CustomerNumber = @CustomerNumberSelectedTo AND 
          HotelNumber = @CFHotelNumber
*/
      IF (@@Error<>0) GOTO Error

      if @CopyProduct=1
        UPDATE CustomerFieldsPerHotel
        SET OvernightProductNumber1=@CFOvernightSingleProduct, OvernightProductNumber2=@CFOvernightGroupProduct, OvernightProductNumber3=@CFOvernightConferenceProduct, OvernightProductNumber4=@CFOvernightWalkInProduct,
            OvernightProductNumber5=@CFOvernightDaymeetingProduct, OvernightProductNumber6=@CFOvernightInternetProduct, OvernightProductNumber7=@CFOvernightOtherProduct1, OvernightProductNumber8=@CFOvernightOtherProduct2,
            DayProductNumber1=@CFDaySingleProduct, DayProductNumber2=@CFDayGroupProduct, DayProductNumber3=@CFDayConferenceProduct, DayProductNumber4=@CFDayWalkInProduct,
            DayProductNumber5=@CFDayDaymeetingProduct, DayProductNumber6=@CFDayInternetProduct, DayProductNumber7=@CFDayOtherProduct1, DayProductNumber8=@CFDayOtherProduct2
        WHERE 
          CustomerNumber = @CustomerNumberSelectedTo AND 
          HotelNumber = @CFHotelNumber

      IF (@@Error<>0) GOTO Error

      IF @CopyNextProduct=1
        UPDATE CustomerFieldsPerHotel
        SET NextOvernightProductNumber1=@CFNextOvernightSingleProduct, NextOvernightProductNumber2=@CFNextOvernightGroupProduct, NextOvernightProductNumber3=@CFNextOvernightConferenceProduct, NextOvernightProductNumber4=@CFNextOvernightWalkInProduct,
            NextOvernightProductNumber5=@CFNextOvernightDaymeetingProduct, NextOvernightProductNumber6=@CFNextOvernightInternetProduct, NextOvernightProductNumber7=@CFNextOvernightOtherProduct1, NextOvernightProductNumber8=@CFNextOvernightOtherProduct2,
            NextDayProductNumber1=@CFNextDaySingleProduct, NextDayProductNumber2=@CFNextDayGroupProduct, NextDayProductNumber3=@CFNextDayConferenceProduct, NextDayProductNumber4=@CFNextDayWalkInProduct,
            NextDayProductNumber5=@CFNextDayDaymeetingProduct, NextDayProductNumber6=@CFNextDayInternetProduct, NextDayProductNumber7=@CFNextDayOtherProduct1, NextDayProductNumber8=@CFNextDayOtherProduct2,
            ProductDate1=@CFDateSingleProductChange, ProductDate2=@CFDateGroupProductChange, ProductDate3=@CFDateConferenceProductChange, ProductDate4=@CFDateWalkInProductChange,
            ProductDate5=@CFDateDaymeetingProductChange, ProductDate6=@CFDateInternetProductChange, ProductDate7=@CFDateOtherProduct1Change, ProductDate8=@CFDateOtherProduct2Change
        WHERE 
          CustomerNumber = @CustomerNumberSelectedTo AND 
          HotelNumber = @CFHotelNumber

      IF (@@Error<>0) GOTO Error

      IF @CopyDiscounts = 1  -- Discount
        UPDATE CustomerFieldsPerHotel 
        SET 
          DiscountAll = @DiscountAll, 
          DiscountRooms = @DiscountRooms, 
          DiscountHalls = @DiscountHalls, 
          DiscountMetting = @DiscountMeeting, 
          DiscountPhone = @DiscountPhone, 
          DiscountPayTv = @DiscountPayTv
        WHERE 
          CustomerNumber = @CustomerNumberSelectedTo AND 
          HotelNumber = @CFHotelNumber

      IF (@@Error<>0) GOTO Error

    FETCH NEXT FROM CustomerFieldsPerHotelCur
    INTO @CFCustomerNumber, @CFHotelNumber, 
         @CFOvernightSingleProduct, @CFOvernightGroupProduct, @CFOvernightConferenceProduct, @CFOvernightWalkInProduct,
         @CFOvernightDaymeetingProduct, @CFOvernightInternetProduct, @CFOvernightOtherProduct1, @CFOvernightOtherProduct2,
         @CFNextOvernightSingleProduct, @CFNextOvernightGroupProduct, @CFNextOvernightConferenceProduct, @CFNextOvernightWalkInProduct,
         @CFNextOvernightDaymeetingProduct, @CFNextOvernightInternetProduct, @CFNextOvernightOtherProduct1, @CFNextOvernightOtherProduct2,
         @CFDaySingleProduct, @CFDayGroupProduct, @CFDayConferenceProduct, @CFDayWalkInProduct, 
         @CFDayDaymeetingProduct, @CFDayInternetProduct, @CFDayOtherProduct1, @CFDayOtherProduct2,
         @CFNextDaySingleProduct, @CFNextDayGroupProduct, @CFNextDayConferenceProduct, @CFNextDayWalkInProduct,
         @CFNextDayDaymeetingProduct, @CFNextDayInternetProduct, @CFNextDayOtherProduct1, @CFNextDayOtherProduct2,
         --@CFAllotment1, @CFAllotment2, @CFAllotment3, @CFAllotment4,
         @CFDateSingleProductChange, @CFDateGroupProductChange, @CFDateConferenceProductChange, @CFDateWalkInProductChange,
         @CFDateDaymeetingProductChange, @CFDateInternetProductChange, @CFDateOtherProduct1Change, @CFDateOtherProduct2Change,
         @DiscountAll, @DiscountRooms, @DiscountHalls, @DiscountMeeting, @DiscountPhone, @DiscountPayTv

    END
  END

       if ((SubString(@CustomerProfile,4,1)='1')AND(@CustomerTypeNumber<7))  -- PaymentTerm
       begin
           if @CustomerTypeNumberSelectedTo=1
            Update Customer set PaymentTypeWareNumber=@PaymentType, PaymentAfterMonth=@PaymentAfterMonth, PaymentAfterDays=@PaymentAfterDays, CreditMax = @CreditMax
            where current of CopyCurTo--CustomerNumber=@CustomerNumberSelectedTo
        else
         Update Customer set PaymentTypeWareNumber=@PaymentType , PaymentAfterMonth=@PaymentAfterMonth, PaymentAfterDays=@PaymentAfterDays, CreditMax = @CreditMax , CurrencyNumber=@Currency
         where current of CopyCurTo--CustomerNumber=@CustomerNumberSelectedTo
       end
       if @@Error<>0 GOTO Error -- GOTO Error Label

       if ((SubString(@CustomerProfile,5,1)='1')AND(@CustomerTypeNumber<7))  -- Provision
       begin
         Update Customer set ProvisionBill = @ProvisionBill, ProvisionAccount = @ProvisionAccount, NetCash = @ProvisionNetCash, Restaurant = @ProvisionRestaurant
         where current of CopyCurTo--CustomerNumber=@CustomerNumberSelectedTo
       end
       if @@Error<>0 GOTO Error -- GOTO Error Label
 
       if SubString(@CustomerProfile,6,1)='1'  -- MarketSegment
         Update Customer set SegmentNumber=@MarketSegment
         where current of CopyCurTo--CustomerNumber=@CustomerNumberSelectedTo    
       if @@Error<>0 GOTO Error -- GOTO Error Label

       if SubString(@CustomerProfile,7,1)='1'  -- POS
         Update Customer set PointOfSaleNumber=@POS
         where current of CopyCurTo--CustomerNumber=@CustomerNumberSelectedTo    --ReadPast
       if @@Error<>0 GOTO Error -- GOTO Error Label


       if SubString(@CustomerProfile,8,1)='1'  -- Purpose
         Update Customer set PurposeCodeNumber=@Purpose
         where current of CopyCurTo--CustomerNumber=@CustomerNumberSelectedTo
       if @@Error<>0 GOTO Error -- GOTO Error Label

       if SubString(@CustomerProfile,9,1)='1'  -- GuestType
         Update Customer set GuestTypeNumber=@GuestType
         where current of CopyCurTo--CustomerNumber=@CustomerNumberSelectedTo    
       if @@Error<>0 GOTO Error -- GOTO Error Label

       if SubString(@CustomerProfile,10,1)='1' -- Notes
       begin
			declare @sql varchar(8000)
			declare @sql1 varchar(8000)
			declare @note varchar(8000)

			set @note = ''
			set @sql = ''
			set @sql1 = ''
			select @note = Contents FROM NoteContents WITH (NOLOCK) WHERE Customernumber = @CustomerNumberFrom AND pagenumber = 1
			set @sql = 'UPDATE NoteContents SET Contents = ''' + @note + ''' WHERE Customernumber = ' + convert(varchar(8), @CustomerNumberTo) + ' AND pagenumber = 1'
--			print @sql
			execute (@sql)
			if @@Rowcount = 0 
			begin
			  set @sql1 = 'INSERT INTO NoteContents (CustomerNumber, PageNumber, Contents) VALUES (' + convert(varchar(8), @CustomerNumberTo) +', 1, ''' + @note + ''')'
--			  print @sql1
			  execute (@sql1)
			  --execute (@sql)
			end

			set @note = ''
			set @sql = ''
			set @sql1 = ''
			select @note = Contents FROM NoteContents WITH (NOLOCK) WHERE Customernumber = @CustomerNumberFrom AND pagenumber = 2
			set @sql = 'UPDATE NoteContents SET Contents = ''' + @note + ''' WHERE Customernumber = ' + convert(varchar(8), @CustomerNumberTo) + ' AND pagenumber = 2'
--			print @sql
			execute (@sql)
			if @@Rowcount = 0 
			begin
			  set @sql1 = 'INSERT INTO NoteContents (CustomerNumber, PageNumber, Contents) VALUES (' + convert(varchar(8), @CustomerNumberTo) +', 2, ''' + @note + ''')'
--			  print @sql1
			  execute (@sql1)
			  --execute (@sql)
			end

			set @note = ''
			set @sql = ''
			set @sql1 = ''
			select @note = Contents FROM NoteContents WITH (NOLOCK) WHERE Customernumber = @CustomerNumberFrom AND pagenumber = 3
			set @sql = 'UPDATE NoteContents SET Contents = ''' + @note + ''' WHERE Customernumber = ' + convert(varchar(8), @CustomerNumberTo) + ' AND pagenumber = 3'
--			print @sql
			execute (@sql)
			if @@Rowcount = 0 
			begin
			  set @sql1 = 'INSERT INTO NoteContents (CustomerNumber, PageNumber, Contents) VALUES (' + convert(varchar(8), @CustomerNumberTo) +', 3, ''' + @note + ''')'
--			  print @sql1
			  execute (@sql1)
			  --execute (@sql)
			end		
       end 

       if @@Error<>0 GOTO Error -- GOTO Error Label    

       if SubString(@CustomerProfile,11,1)='1'  -- IATANumber
         if @CustomerTypeNumberSelectedTo<>1
            Update Customer set IATANumber=@IATANumber
            where current of CopyCurTo--CustomerNumber=@CustomerNumberSelectedTo    
       if @@Error<>0 GOTO Error -- GOTO Error Label       
       
       /*
       if SubString(@CustomerProfile,14,1)='1'  -- Email+WWW
         Update Customer set Email=@EmailAdd , WWWAddress=@WWWadd
         where current of CopyCurTo--CustomerNumber=@CustomerNumberSelectedTo    
       if @@Error<>0 GOTO Error -- GOTO Error Label
       */
       
       IF SUBSTRING (@CustomerProfile, 14, 1) = '1'  -- EMail & Web
       BEGIN          
       
         UPDATE Customer 
         SET WWWAddress = @WWWadd
         WHERE CURRENT OF CopyCurTo
         
         DELETE FROM MAILADDRESSES
         WHERE CustomerNumber = @CustomerNumberTo
         
         DECLARE @MailAddress VARCHAR(100)  
         DECLARE @MailAddressType TINYINT  
               
         DECLARE CopyMailAddressesCursor Cursor FOR
           SELECT MailAddress, MailAddressType 
           FROM MailAddresses WITH (NOLOCK)
           WHERE CustomerNumber = @CustomerNumberFrom
           
         OPEN CopyMailAddressesCursor
         FETCH NEXT FROM CopyMailAddressesCursor 
         INTO @MailAddress, @MailAddressType  
           
         WHILE @@Fetch_status = 0
         BEGIN
           IF RTRIM(LTRIM(ISNULL (@MailAddress, ''))) <> ''
             INSERT INTO MailAddresses (CustomerNumber, MailAddress, MailAddressType) 
               VALUES (@CustomerNumberTo, @MailAddress, @MailAddressType)	
           FETCH NEXT FROM CopyMailAddressesCursor 
           INTO @MailAddress, @MailAddressType 
         END 
         
         CLOSE CopyMailAddressesCursor
         DEALLOCATE CopyMailAddressesCursor          
       END   
       IF @@Error <> 0 GOTO Error  

       if SubString(@CustomerProfile,15,1)='1'  -- CompanyNumber
         if ((@CustomerTypeNumber=2 or  @CustomerTypeNumber=3 or @CustomerTypeNumber=6 or  @CustomerTypeNumber=7) AND
             (@CustomerTypeNumberSelectedTo=2 or  @CustomerTypeNumberSelectedTo=3 or @CustomerTypeNumberSelectedTo=6 or  @CustomerTypeNumberSelectedTo=7))
            Update Customer set CompanyNumber=@CompanyNumber
            where current of CopyCurTo--CustomerNumber=@CustomerNumberSelectedTo
       if @@Error<>0 GOTO Error -- GOTO Error Label

       if SubString(@CustomerProfile,16,1)='1' -- Attachments        
         Exec CopyCustomerAttachments @CustomerNumberFrom, @CustomerNumberSelectedTo 
 
       if @@Error<>0 GOTO Error -- GOTO Error Label

       if SubString(@CustomerProfile,17,1)='1' -- Flags
         Update Customer set FlagA=@FlagA , FlagB=@FlagB , FlagC=@FlagC , FlagD=@FlagD
         where current of CopyCurTo--CustomerNumber=@CustomerNumberSelectedTo
       if @@Error<>0 GOTO Error -- GOTO Error Label

       if ((SubString(@CustomerProfile,18,1)='1')AND(@CustomerTypeNumber<7)) -- SplitBill

         Update Customer set SplitBillNumber=@SplitBill
         where current of CopyCurTo--CustomerNumber=@CustomerNumberSelectedTo
       if (@@Error<>0) GOTO Error -- GOTO Error Label

       --Commit Tran
       SET @NumberOfLinks = @NumberOfLinks + 1
       Insert Into UniversalLog (DateStamp,Time,PrimaryType,SubType,Link,Description,UserID,Atext)Values(@Date,Convert (Char(5),GetDate(),108),3,14,@CustomerNumberSelectedTo,'Customer profile is copied ',@UserID,'From '+@FullName+' [Customer no. '+@StrCustomerNumber+']')
       GOTO NoError
       Error: SET @NumberOfLocks = @NumberOfLocks + 1
       NoError: print 'NOError'

       FETCH NEXT FROM CopyCurTo         
       INTO  
         @CustomerNumberSelectedTo, @CustomerTypeNumberSelectedTo, @PaymentTypeTo, @PaymentAfterMonthTo, 
         @PaymentAfterDaysTo, @CreditMaxTo, @CurrencyTo, @ProvisionBillTo, @ProvisionAccountTo, 
         @ProvisionNetCashTo, @ProvisionRestaurantTo, 
         @MarketSegmentTo, @POSTo, @PurposeTo, @GuestTypeTo, @IATANumberTo, -- @EmailAddTo, 
         @WWWAddTo, @CompanyNumberTo, @FlagATo, @FlagBTo, @FlagCTo, @FlagDTo, @SplitBillTo                      
END

Close CopyCurTo
DeAllocate CopyCurTo

Close CustomerFieldsPerHotelCur
DeAllocate CustomerFieldsPerHotelCur

--SET @NumberOfLocks = (Select Count(CustomerNumber) from #ResultTAB WITH (NOLOCK)) - @NumberOfLinks

Drop Table #FoundValues
Drop Table #ResultTAB