# 关于ASP递归的问题讨论,高手请进!

jnqjj 2005-12-09 05:32:20

sub childnum(byval userid) '递归的遍历函数
dim rs
sql="select userid,shenhe,zcdata from user where anid="&userid
rs.open sql,conn
Do while not rs.eof
if rs("shenhe")=1 then
k=k+1 '计数器+1
end if
call childnum(rs("userid"))
rs.movenext
Loop
rs.close
set rs=nothing
end sub '函数结束
...全文
109 点赞 收藏 10

10 条回复

patchclass 2005-12-10

ShiningstarHu 2005-12-10

sub childnum(byval userid) '递归的遍历函数
sql="select userid,shenhe,zcdata from user where anid="&userid
rs.open sql,conn
Do while not rs.eof
if rs("shenhe")=1 then
k=k+1 '计数器+1
end if
call childnum(rs("userid"))
rs.movenext
Loop
rs.close
end sub '函数结束

dh20156 2005-12-09

Set Nocount On
Create Table test(userid int,shenhe int,anid int)
Insert into test Select 1,1,0
Union Select 2,1,0
Union Select 3,1,1
Union Select 4,1,1
Union Select 5,1,2
Union Select 6,1,2
Union Select 7,1,3
Union Select 8,0,4

Create Table #t ([id] int,[level] int,sid varchar(100))
Declare @l int
Set @l=0
Insert into #t select [userid],@l,[userid] From [test] Where test.anid=0
While @@rowcount>0
Begin
Set @l=@l+1
Insert into #t select a.[userid],@l,CAST(b.sid as varchar)+','+CAST(a.[userid] as varchar) from [test] a,#t b where a.[anid]=b.[id] and b.[level]=@l-1 and a.shenhe=1
End

Set Nocount Off

Select test.userid,Count(1) As tc From #t,test Where charindex(CAST(test.userid as varchar),#t.sid)>0 Group by test.userid

Drop Table #t
Drop Table test

--结果：
/*
userid tc
----------- -----------
1 4
2 3
3 2
4 1
5 1
6 1
7 1

（所影响的行数为 7 行）
*/

dh20156 2005-12-09

jnqjj 2005-12-09

KimSoft 2005-12-09

jnqjj 2005-12-09

KimSoft 2005-12-09

* 性能：当正确使用时，数据整形可以改善性能。
* 便利：在数据整形中非常容易映射父子关系。

http://www.itll.net/Article/softtech/Website/ASP/jc/200506/32116.html

KimSoft 2005-12-09
oracle里有个语法能取出树形结构，今天上午，刚写了一个，如果是Access和sqlserver，可以使用dataSharp取出树形结构。这样应该快很多。代码我没有写。

select
from
province_list
par_index=0
connect by
prior p_index=par_index

ASP

2.8w+

ASP即Active Server Pages，是Microsoft公司开发的服务器端脚本环境。

2005-12-09 05:32