



CREATE PROCEDURE DBO.AllUpLinksAgent AS
SET NOCOUNT ON

  Insert #TempTab(CustomerNumber,CustomerTypeNumber,ProcessingNumber) -- finder alle saleschannels og agencyCorp
  select CustomerNumber,CustomerTypeNumber,1 from Customer WITH (NOLOCK) where (CustomerTypeNumber=5 or CustomerTypeNumber=7) and CustomerNumber in
  (Select CustomerNumber from CustomerLinks WITH (NOLOCK) where LinkTypeNumber=0 and CustomerLinkNumber in (select  CustomerNumber from #FoundValues where CustomerTypeNumber=3 ) UNION ALL
  Select CustomerLinkNumber from CustomerLinks WITH (NOLOCK) where LinkTypeNumber=0 and  CustomerNumber in  (select  CustomerNumber from #FoundValues where CustomerTypeNumber=3))

  Insert #TempTab(CustomerNumber,CustomerTypeNumber,ProcessingNumber) -- finder alle saleschannel som er linket til agencyCorp
  select CustomerNumber,CustomerTypeNumber,1 from Customer WITH (NOLOCK) where CustomerTypeNumber=7 and CustomerNumber in
  (Select CustomerNumber from CustomerLinks WITH (NOLOCK) where LinkTypeNumber=0 and CustomerLinkNumber in (select  CustomerNumber from #TempTab where CustomerTypeNumber=5) UNION ALL
  Select CustomerLinkNumber from CustomerLinks WITH (NOLOCK) where LinkTypeNumber=0 and  CustomerNumber in  (select  CustomerNumber from #TempTab where CustomerTypeNumber=5))

