SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 query help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

esthera
Flowing Fount of Yak Knowledge

1388 Posts

Posted - 11/13/2012 :  08:16:44  Show Profile  Reply with Quote
I have a table called
category
with
catid
catname
parentcatid



I want to pass in a cat id and do a query that will return to me all th parents names in order (looking by parentcatid until parentcatid is 0)

what's the best way to do it? a function?

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/13/2012 :  08:32:53  Show Profile  Reply with Quote
Probably use a recursive CTE
;WITH cte AS
(
	SELECT catId,catName FROM Category WHERE catid = @catid
	UNION ALL
	SELECT c.catId,c.CatName FROM 
	Category c INNER JOIN cte t ON t.parentCatid = c.catId
)SELECT * FROM cte;
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000