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
BEGIN

	SET NOCOUNT ON

	/*
	Changes:
	  MLJ 2020-10-20: Copy notes: append or replace
	  vd 20190919: Provision values now taken from CustomerFieldsPerHotel.
	  vd 20180828: CLN. Housekeeping.
	  vd 20160829: Bug. Table Universallog altered.
	  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.
	  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 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 @MarketSegment Int
	DECLARE @POS int
	DECLARE @Purpose int
	DECLARE @GuestType int
	DECLARE @IATANumber VarChar(30)
	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 @ProvisionBill MONEY
	DECLARE @ProvisionAccount MONEY
	DECLARE @ProvisionRestaurant MONEY
	DECLARE @ProvisionNetto BIT
	DECLARE @ProvisionPostOnGuest BIT

	DECLARE @DiscountAll MONEY
	DECLARE @DiscountRooms MONEY
	DECLARE @DiscountHalls MONEY
	DECLARE @DiscountMeeting MONEY
	DECLARE @DiscountPhone MONEY
	DECLARE @DiscountPayTv MONEY

	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 @MarketSegmentTo Int
	DECLARE @POSTo int
	DECLARE @PurposeTo int
	DECLARE @GuestTypeTo int
	DECLARE @IATANumberTo VarChar(30)
	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 @CurDT DATETIME

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

	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
		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 @CopyDiscountsAndProvisons BIT

	IF SUBSTRING(@CustomerProfile, 1, 1) = '1'
	  SET @CopyAllotments = 1
	ELSE
	  SET @CopyAllotments = 0

	IF SUBSTRING(@CustomerProfile, 2, 1) = '1'
	  SET @CopyProduct = 1
	ELSE
	  SET @CopyProduct = 0

	IF SUBSTRING(@CustomerProfile, 3, 1) = '1'
	  SET @CopyNextProduct = 1
	ELSE
	  SET @CopyNextProduct = 0

	IF SUBSTRING(@CustomerProfile, 5, 1) = '1'
	  SET @CopyDiscountsAndProvisons = 1
	ELSE
	  SET @CopyDiscountsAndProvisons = 0

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

	SELECT 
	  @PaymentType = PaymentTypeWareNumber, @PaymentAfterMonth = PaymentAfterMonth, 
	  @PaymentAfterDays = PaymentAfterDays, @CreditMax = CreditMax, @Currency = CurrencyNumber,
	  @MarketSegment = SegmentNumber, @POS = PointOfSaleNumber, @Purpose = PurposeCodeNumber, @GuestType = GuestTypeNumber,
	  @IATANumber = IATANumber, @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,
	  ProvisionBill, ProvisionAccount, ProvisionRestaurant, ProvisionNetto, ProvisionPostOnGuest,  
	  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,
	  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, 
		SegmentNumber, PointOfSaleNumber, PurposeCodeNumber, GuestTypeNumber, IATANumber,  
		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,
	  @MarketSegmentTo, @POSTo, @PurposeTo, @GuestTypeTo, @IATANumberTo, 
	  @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 @CopyProduct = 1 OR @CopyNextProduct = 1 OR @CopyDiscountsAndProvisons = 1
	  BEGIN

		FETCH FIRST FROM CustomerFieldsPerHotelCur 
		INTO 
		  @CFCustomerNumber, @CFHotelNumber, 
		  @ProvisionBill, @ProvisionAccount, @ProvisionRestaurant, @ProvisionNetto, @ProvisionPostOnGuest,
		  @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,
		  @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 (@@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 @CopyDiscountsAndProvisons = 1  -- Discount and Provision
			UPDATE CustomerFieldsPerHotel
			SET
			  ProvisionBill = @ProvisionBill,
			  ProvisionAccount = @ProvisionAccount,
			  ProvisionRestaurant = @ProvisionRestaurant,
			  ProvisionNetto = @ProvisionNetto,
			  ProvisionPostOnGuest = @ProvisionPostOnGuest,
			  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, 
		  @ProvisionBill, @ProvisionAccount, @ProvisionRestaurant, @ProvisionNetto, @ProvisionPostOnGuest,
		  @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,
		  @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
			else
			 Update Customer set PaymentTypeWareNumber=@PaymentType , PaymentAfterMonth=@PaymentAfterMonth, PaymentAfterDays=@PaymentAfterDays, CreditMax = @CreditMax , CurrencyNumber=@Currency
			 where current of CopyCurTo
		   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
		   if @@Error<>0 GOTO Error -- GOTO Error Label

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


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

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

		   if SubString(@CustomerProfile,10,1)<>'0' -- Notes
		   begin
				declare @AppendNotes bit = 1;
				if SubString(@CustomerProfile,10,1)='2'
					SET @AppendNotes = 0;
				MERGE NoteContents T
				USING (SELECT @CustomerNumberTo CustomerNumber, Contents, PageNumber FROM NoteContents WITH (NOLOCK) WHERE CustomerNumber = @CustomerNumberFrom) S
				ON (T.CustomerNumber=S.CustomerNumber AND T.PageNumber=S.PageNumber)
				WHEN MATCHED THEN
					UPDATE SET Contents=CASE WHEN @AppendNotes=1 THEN T.Contents+CHAR(13)+CHAR(10)+ S.Contents ELSE S.Contents END
				WHEN NOT MATCHED BY TARGET THEN
					INSERT (CustomerNumber, Contents,PageNumber) VALUES (S.CustomerNumber, S.Contents,S.PageNumber)
				;
		   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 
		   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
		   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
		   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
		   if (@@Error<>0) GOTO Error -- GOTO Error Label

		   --Commit Tran
		   SET @NumberOfLinks = @NumberOfLinks + 1

       EXEC SystemDateTime '', @CurDT OUTPUT

		   INSERT INTO UniversalLog (DateStamp, Time, SubType, Link, Description, UserID, Atext)
		   VALUES (@Date, Convert (Char(5), @CurDT, 108), 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, 
			 @MarketSegmentTo, @POSTo, @PurposeTo, @GuestTypeTo, @IATANumberTo,  
			 @WWWAddTo, @CompanyNumberTo, @FlagATo, @FlagBTo, @FlagCTo, @FlagDTo, @SplitBillTo                      
	END

	CLOSE CopyCurTo
	DEALLOCATE CopyCurTo

	CLOSE CustomerFieldsPerHotelCur
	DEALLOCATE CustomerFieldsPerHotelCur

	DROP TABLE #FoundValues
	DROP TABLE #ResultTAB
END
