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
229 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
ID: 36568078
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
ID: 36568101
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
ID: 36568131
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:amoses
ID: 36954324
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
ID: 36954343
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
ID: 36956434
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

792 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