



CREATE PROCEDURE DBO.AllUplinksCompany  AS
SET NOCOUNT ON

Insert #TempTab(CustomerNumber,CustomerTypeNumber,ProcessingNumber) -- Finder alle saleschannel der er linket til CompanyCorp
  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 #FoundValues where CustomerTypeNumber=4 ) UNION ALL
  Select CustomerLinkNumber from CustomerLinks WITH (NOLOCK) where LinkTypeNumber=0 and CustomerNumber in  (select CustomerNumber from #FoundValues where CustomerTypeNumber=4 ))

Insert #TempTab(CustomerNumber,CustomerTypeNumber,ProcessingNumber) -- Finder alle saleschannel  og agencycorp der er linket til agency
  Select CustomerNumber,CustomerTypeNumber,1 from Customer WITH (NOLOCK) where (CustomerTypeNumber=7 or CustomerTypeNumber=5)  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 der 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 ))

