| Author |
Topic |
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2009-09-27 : 12:50:52
|
| My Table contains following fieldsCodeNo,SponsorCodeThis is referal program. A guy refers to 2 guys and they 2 refers another 2-2=4.I wan't to count complete downline of any code say 2.CodeNo SponsorCode1 02 13 14 25 26 3by this if we count the downline of 1 then it would be 5. If we count the downline of 2 then it would be 3 & the downline of 3 would be 1 only.I've to write STORED PROCEDURE in SQL Server. Can anyone helps. I'm new in STORED PROCEDURE. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-27 : 21:49:35
|
[code]-- Sample Table & DataDECLARE @sample TABLE( CodeNo int, SponsorCode int)INSERT INTO @sampleSELECT 1, 0 UNION ALLSELECT 2, 1 UNION ALLSELECT 3, 1 UNION ALLSELECT 4, 2 UNION ALLSELECT 5, 2 UNION ALLSELECT 6, 3-- Query; WITH dataAS( SELECT CodeNo, SponsorCode = CodeNo FROM @sample WHERE CodeNo = 1 UNION ALL SELECT s.CodeNo, d.SponsorCode FROM @sample s INNER JOIN data d ON s.SponsorCode = d.CodeNo)SELECT no_downline = COUNT(*)FROM dataWHERE CodeNo <> SponsorCode-- Result/*no_downline ----------- 5(1 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2009-09-27 : 22:55:33
|
| INSERT INTO @sampleSELECT 1, 0 UNION ALLSELECT 2, 1 UNION ALLSELECT 3, 1 UNION ALLSELECT 4, 2 UNION ALLSELECT 5, 2 UNION ALLSELECT 6, 3I'm not getting |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-27 : 23:17:03
|
that is just for me to simulate your environment. I don't have your database or table or data to write the query KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2009-09-28 : 01:52:12
|
| I'm quite new in SQL STORED PROCEDURES. What would be the exact query to count downline members of 1 from my database. Thanks for reply. Kindly Help. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-28 : 02:11:05
|
just that part after the line-- querychange @sample to your actual table name in that query KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2009-09-28 : 06:22:04
|
| If I use { & } SQL STORED PROCEDURE gives error "Syntax error or sharing violation" It's find if I remove it. When I run the query I got error message "Invalid object name 'data'.". I think we are near to Solution. |
 |
|
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2009-09-28 : 06:27:00
|
| Here is my Syntax which I'd written in SQL.CREATE PROCEDURE FindDownline (@CodeNo_1 [numeric](9))AsSelect CodeNo,SponsorCode=CodeNo from MyTableName Where CodeNo=@CodeNo_1 union all Select s.CodeNo,d.SponsorCode FROM MyTableName s INNER JOIN data d ON s.SponsorCode=d.CodeNoSelect no_downline = count(*) from data where CodeNo<> SponsorCodeGO |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-28 : 06:39:06
|
[code]CREATE PROCEDURE FindDownline(@CodeNo_1 [numeric](9))As; WITH dataAS( SELECT CodeNo, SponsorCode = CodeNo FROM MyTableName WHERE CodeNo = @CodeNo_1 UNION ALL SELECT s.CodeNo, d.SponsorCode FROM MyTableName s INNER JOIN data d ON s.SponsorCode = d.CodeNo)SELECT no_downline = COUNT(*)FROM dataWHERE CodeNo <> SponsorCode[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2009-09-28 : 06:45:43
|
| Thanx for immediate replystill it doesn't worksOne more thing I'm using Sql Server 2000 and Not 2005 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-28 : 09:44:46
|
well you posted in Transac-SQL (2005) forum and i posted a solution for SQL 2005. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-09-28 : 10:50:19
|
for SQL 2000, use a loop to count it KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
ravininave
Posting Yak Master
111 Posts |
Posted - 2009-09-28 : 16:40:11
|
| Well khtan, thanks for your valueable suggestions. I install 2005 and the query works fine. Thanks. |
 |
|
|
micnie_2020
Posting Yak Master
232 Posts |
Posted - 2009-10-23 : 00:29:36
|
| Hi all,Could anyone please provide the code which worable in mssql 2000 for above issue?Thank you.Regards,Michelle |
 |
|
|
|