troubleshooting Question

hierarchy return all children

Avatar of johnson1
johnson1 asked on
Microsoft SQL Server 2008
3 Comments1 Solution299 ViewsLast Modified:

I have a hierarchy table like:

DECLARE @MyTable table(Id int, ParentId int)

INSERT INTO @MyTable(Id,ParentId)values(1,0)
INSERT INTO @MyTable(Id,ParentId)values(2,0)
INSERT INTO @MyTable(Id,ParentId)values(3,2)
INSERT INTO @MyTable(Id,ParentId)values(4,3)
INSERT INTO @MyTable(Id,ParentId)values(5,4)
INSERT INTO @MyTable(Id,ParentId)values(6,4)
INSERT INTO @MyTable(Id,ParentId)values(7,0)

select * from @MyTable

If the value of the Id parameter of the procedure is 3,  then I would like to get Id=3, Id=4, Id=5 and Id=6 , i.e. the item itself and all childrens of it.

How is best to do this? (the result can return thousands of rows).

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros