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

Hello,

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
toshi_
Erwin PombettAsked:
Who is Participating?
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:
https://technet.microsoft.com/en-us/library/ms186243%28v=sql.105%29.aspx
0

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.

toshif
0
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]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[func_tshr_OrgZone_getFromIDDown]
(
      @OrgName varchar(255)
)
RETURNS TABLE
AS
RETURN
(
      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
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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

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

Toshi
0
Erwin PombettAuthor Commented:
Hello Again Guy,

hope you still there ...I succeed ;)

thank you for the tip and your help
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
glad to see you got it :)
 and that could help you on your way
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.