In sqlserver how can pass a query, recover values in order to pass a new query, keep all results and return them ?


I have a table that has relation between rows.
Rows have a column [id] and a column [parent_id].
all are in the same table.

how can i recover all rows "down" from an ID like follows  

select id where parent id = 3

then i will receive for instance : 4, 5, 6

i need to do the same query again for each value until there's nothing.

I'd like to create a Function that will receive a parameter with the id on top and receive all values that are found down.

thank you in advance
Erwin PombettAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
in SQL Server, this would be using the recursive CTE technique, explained in the official technet article:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Erwin PombettAuthor Commented:
Hello Guy Hengel !

thank you for the reply,

I'm going to read and try to translate the doc to my query.

Erwin PombettAuthor Commented:
Hello Guy...

i can not succeed yet...

can you help make a few steps ?

this is my first function.
my problem is that

------------------------------ my function --------------------------
USE [myDB]


ALTER FUNCTION [dbo].[func_tshr_OrgZone_getFromIDDown]
      @OrgName varchar(255)
      SELECT @OrgName AS TheOrgRoleName
        FROM OrgZone WHERE Name = @OrgName

if i execute this function i receive : Command(s) completed successfully.

if i call my function as follow :
select dbo.func_tshr_OrgZone_getFromIDDown ("All Business Locations")

i receive :
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.func_tshr_OrgZone_getFromIDDown", or the name is ambiguous.

how can i call my function, this is just the begging to be sure the big picture is ok.

thankk you in advance
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
to call such a function:
select * from dbo.func_tshr_OrgZone_getFromIDDown ("All Business Locations")
Erwin PombettAuthor Commented:

i saw it appear as expected once i've restarted sqlserver.....
and i could use it ( i had tried with - * from - but did not work )   ....not with double quotes  but with single quotes.

i keep reading ;)

Erwin PombettAuthor Commented:
Hello Again Guy,

hope you still there ...I succeed ;)

thank you for the tip and your help
Guy Hengel [angelIII / a3]Billing EngineerCommented:
glad to see you got it :)
 and that could help you on your way
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.