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 2005 Forums
 Transact-SQL (2005)
 Level problem

Author  Topic 

ravininave
Posting Yak Master

111 Posts

Posted - 2009-09-30 : 22:40:47
Hi,
I'm developing a MLM Software. My table structure is
Table Name : MemMast
Fields : CodeNo,SponsorCode,CustName

Sample Data
Code CustName SponsorCode
1 a 0
2 b 1
3 c 1
4 d 3
5 e 2
6 f 5
7 g 4

I want the output for Code No. 1

Code CustName Level
2 b 1
3 c 1
4 d 2
5 e 2
6 f 3
7 g 3

I've read many post like this in this forum. But I haven't found any final answer or perfect query.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-30 : 23:05:19
use Recursive CTE

refer to http://msdn.microsoft.com/en-us/library/ms186243.aspx


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ravininave
Posting Yak Master

111 Posts

Posted - 2009-09-30 : 23:28:09
quote:
Originally posted by khtan

use Recursive CTE

refer to http://msdn.microsoft.com/en-us/library/ms186243.aspx


KH
[spoiler]Time is always against us[/spoiler]





Can u post any good Code.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-30 : 23:35:14
there is a good example there in the link


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ravininave
Posting Yak Master

111 Posts

Posted - 2009-09-30 : 23:47:12
quote:
Originally posted by khtan

there is a good example there in the link


KH
[spoiler]Time is always against us[/spoiler]





I'm a beginner
Go to Top of Page

ravininave
Posting Yak Master

111 Posts

Posted - 2009-10-01 : 00:00:40
Can u post some code according to my database. It could be helpfull.
Go to Top of Page

ravininave
Posting Yak Master

111 Posts

Posted - 2009-10-01 : 00:11:08
Well, thanx. I solved it by this way. Thanx for ur help.


create PROCEDURE findlevel
(@IBDNO_1 [numeric](9))



AS
;with data
AS
(
Select IBDNo,SP_IBDNo,1 as level from IBDDets Where IBDNo=@IBDNo_1
union all
Select s.IBDNo,d.SP_IBDNo,level+1 FROM IBDDets s INNER JOIN data d ON s.SP_IBDNo=d.IBDNo
)
select * from data order by level
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-10-01 : 00:39:46
that's good


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -