troubleshooting Question

hierarchy return all children

Avatar of johnson1
johnson1 asked on
Microsoft SQL Server 2008
3 Comments2 Solutions299 ViewsLast Modified:
Hello,

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

Open in new window


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).

Greetings
ASKER CERTIFIED SOLUTION
Surendra Nath
Technology Lead

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 3 Comments.
Start Free Trial
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 2 Answers 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