Solved

Is there any work around to use a Left Join in a SQL Server CTE in the recursive member

Posted on 2011-09-20
6
219 Views
Last Modified: 2012-05-12
I have a CTE and need to use a LEFT join in the recursive member. I get the error stating that an outer join in not possible in the recursive member. Is there any work around to this limitation?

Below is the simplified query I'd like to run.
;WITH cte (name, CatalogID, parentcatalogid) AS
          (   select CI.[Name], CI.CatalogID, NULL as ParentCatalogID
              from [CatalogInfo] CI
          union all  
              select convert(varchar(max),cte.name+' | '+ (COALESCE(C.[Name], CLc.[Name]))) , C.catalogid, C.parentcatalogid
              from [Catalog] as C 
              LEFT join CatalogLC CLc ON CLc.CatalogID = C.CatalogID AND CLc.LCID = 1033
              inner join cte on C.parentcatalogid=cte.[catalogid]
	)
    SELECT cte.CatalogID, cte.name 
    from cte

Open in new window

0
Comment
Question by:amoses
  • 3
  • 2
6 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
Hi. You can try putting that in its own CTE. Then just reference it in the recursive one.

;WITH c(name, CatalogID, parentcatalogid) AS (
SELECT COALESCE(C.[Name], CLc.[Name]), C.catalogid, C.parentcatalogid
FROM [Catalog] as C 
LEFT JOIN CatalogLC CLc ON CLc.CatalogID = C.CatalogID AND CLc.LCID = 1033
), cte (name, CatalogID, parentcatalogid) ...

Open in new window


Hope that helps!
0
 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
Can you show us some sample data from each of the three tables?  thanks.
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 total points
Comment Utility
Sorry I was in a rush. In the second CTE, you would do an INNER JOIN:

from c
inner join cte on c.parentcatalogid=cte.[catalogid]


And so, you avoid the OUTER JOIN in recursive piece and life is good. :)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:amoses
Comment Utility
Sorry for the delay in getting back.

I created a variable with the CTE and inserted it into a table variable. Thanks mwvisa1 for pointing me in the right direction.

DECLARE @LeafNodes TABLE (CatalogID INT);
DECLARE @vSQL nvarchar(MAX);

SET @vSQL = '
        WITH cte (name, CatalogID, parentcatalogid) AS
          (   select CI.[Name], CI.CatalogID, NULL as ParentCatalogID
              from [CatalogInfo] CI
          union all  
              select convert(varchar(max),cte.name+' | '+ (COALESCE(C.[Name], CLc.[Name]))) , C.catalogid, C.parentcatalogid
              from [Catalog] as C 
              LEFT join CatalogLC CLc ON CLc.CatalogID = C.CatalogID AND CLc.LCID = 1033
              inner join cte on C.parentcatalogid=cte.[catalogid]
	)
    SELECT cte.CatalogID from cte'  

INSERT INTO @LeafNodes (CatalogID)
exec (@vSQL)

Open in new window

0
 

Author Closing Comment

by:amoses
Comment Utility
I adapted mwvisa1's solution to fit my needs and posted it so it can help others.
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
Glad to help. By the way, you probably do not need the dynamic SQL (see attached). You still show the LEFT JOIN, but the point I am making below is you can have the INSERT as part of the final selection.

Best regards and happy coding,

Kevin
DECLARE @LeafNodes TABLE (CatalogID INT);
;WITH cte (name, CatalogID, parentcatalogid) AS
(
select CI.[Name], CI.CatalogID, NULL as ParentCatalogID
from [CatalogInfo] CI
union all  
select convert(varchar(max),cte.name+' | '+ (COALESCE(C.[Name], CLc.[Name]))) , C.catalogid, C.parentcatalogid
from [Catalog] as C 
left join CatalogLC CLc ON CLc.CatalogID = C.CatalogID AND CLc.LCID = 1033
inner join cte on C.parentcatalogid=cte.[catalogid]
)
INSERT INTO @LeafNodes (CatalogID)
SELECT cte.CatalogID from cte
;

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now