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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Question about Recursive SQL from bottom up

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-02-10 : 19:30:59
Mark writes "Given the following table:

TABLE FOO:
String parentOID
String childOID


I need to create a recursive sql statement in which given a childOID, I need to get all of its parents, and all of its parents parents.

Can you help?"

aiken
Aged Yak Warrior

525 Posts

Posted - 2002-02-10 : 22:52:20
I haven't tested this (or even checked for typos), but I think you're looking for something like this, assuming you're looking for a comma delited answer

select dbo.f_Parents('childOID')

----
CREATE FUNCTION dbo.f_Parents(@childOID int) AS
returns varchar(500)

DECLARE @vcRet varchar(500),vcParent varchar(500)

select @vcParent=parentOID from FOO where childOID=@childOID
if @vcParent is not null
select @vcRet=dbo.f_Parents(@vcParent) + ',' + @vcParent

return @vcRet

Go to Top of Page
   

- Advertisement -