Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 parent/child relation

Author  Topic 

sparrow37
Posting Yak Master

148 Posts

Posted - 2010-12-01 : 02:10:07
Hi All:

I have table with id, name, parentid. I want to create a stored procedure/ sql to which i pass the id and it returns all the child recordids, and name related to this id( i guess its parent id).

Regards,
Asif Hameed

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-12-01 : 02:43:54
Use Recursive CTE For this -
This link can help you -
http://www.sqlservercurry.com/2009/06/simple-family-tree-query-using.html

Vaibhav T

If I cant go back, I want to go fast...
Go to Top of Page

sparrow37
Posting Yak Master

148 Posts

Posted - 2010-12-01 : 03:25:56
Hi Vaibhav:

Can i do it without CTE?

Regards,
Asif Hameed
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-12-01 : 12:28:42
you can, but you need a loop to loop through levels each time. Why do you want to avoid CTEs? Are you in 2000 or below?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -