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)
 Recursive query ?!?

Author  Topic 

brichardson
Starting Member

1 Post

Posted - 2003-09-12 : 22:54:37
I have a problem that is probably rather well known in the MLM software industry.

I have a table of users called (surprise) Users, which amongst other columns, has an ID column.

I have a linking table called User_Uplines, which contains the foreign keys FK_UserID (which points to the Users.ID column of the User), and FK_UplineID (which points to the Users.ID column of the User's Upline Sponsor).

So, the query:

Select * from User_Uplines where FK_UplineID = @UserID

will return all of a user's FIRST LEVEL subordinates.

The problem is, I'd like to return ALL of the User's downlines' downlines, traversing every branch, until there are no more. I could certainly do this with a (rather nasty) cursor, or bring the recursive function into C# and make a BUNCH of db calls to get the first level of each user, and recursive through a collection until I reach the last entry. BUT, is there any way to write a recursive routine in SQL that would do this?

Many thanks in advance, and if you need any more info, please let me know.

Thanks,

Bill

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-09-13 : 09:49:54
Check this out for some info:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25964

- Jeff
Go to Top of Page
   

- Advertisement -