求一个递归函数的写法
表cc 有字段 a b
一函数名:str_SQL,有一个传入参数:st, 一个返回值:ls
查询cc 当b = st |
如果有数据. |----得到数据的条数不定
得到a的数据.把a赋给st |
|
继续查询cc 当b=st |
如果无数据 |
得到a的数据. |
|
返回 ls =a |----例:最后查出4条数据,则ls = a(第1条)+a(第2条)+a(第3条)+a(第4条)
写了半天老是出错..vb.net的...请大虾们帮忙..
问题点数:50、回复次数:10Top
1 楼aoyo(遨游~ 认真生活,努力微笑^_^)回复于 2005-01-14 15:54:38 得分 0
你是写存储过程还是写程序呀,看不大明白你的描述
得到数据的条数不定-->你怎么在“把a赋给st”Top
2 楼lovecx7758521(简爱)回复于 2005-01-14 15:58:20 得分 0
比如第一次查出2条,a=4,a=5 那么.
st = 4 查询cc当b=st
st = 5 查询cc当b=stTop
3 楼lovecx7758521(简爱)回复于 2005-01-14 16:15:50 得分 0
例:表cc中数据
a b
1 0
2 0
3 1
4 1
5 2
6 3
7 4
8 7
传入参数=1
查询 b=1 得到a=3 a=4 分别把这2个值赋给b 继续
查询 b=3 得到a=6 把6赋给b 继续
查询 b=6 无数据
查询 b=4 得到a=7 把7赋给b 继续
查询 b=7 得到a=8 把8付给b 继续
查询b=8 无数据
返回值:6+7+8 (字符串)Top
4 楼lovecx7758521(简爱)回复于 2005-01-14 16:33:35 得分 0
分不够..我加加Top
5 楼Aspx_CC()回复于 2005-01-14 17:00:29 得分 0
7是怎么来的,条件没说清楚啊Top
6 楼lovecx7758521(简爱)回复于 2005-01-14 17:04:41 得分 0
a b
1 0
2 0
3 1
4 1
5 2
6 3
7 4
8 7
当b=4的时候.a=7Top
7 楼Aspx_CC()回复于 2005-01-14 17:14:42 得分 0
那为什么没有3,4Top
8 楼bluesmile979(笑着)回复于 2005-01-14 17:16:49 得分 25
大概逻辑如下,geta(st)里面封装的是你根据b的值取得a的逻辑
public function sample(byval st as string) as string
dim ls as string = ""
dim aset() as string = geta(st)
dim a as string
for each a in aset
ls = ls & a
ls = ls & sample(a)
next
return ls
end functionTop
9 楼lovecx7758521(简爱)回复于 2005-01-14 17:19:32 得分 0
3是6的上层
4是7的上层
7是8的上层
应该返回 6 + 8Top
10 楼Aspx_CC()回复于 2005-01-14 17:33:29 得分 25
set nocount on
declare @CC table( a int, b int )
insert into @CC
select 1,0
union select 2 , 0
union select 3 , 1
union select 4 , 1
union select 5 , 2
union select 6 , 3
union select 7 , 4
union select 8 , 7
declare @Input int
set @Input = 1
declare @InputTable table( a int, b int )
insert into @InputTable
select a, b from @CC where b = @Input
while( @@rowcount != 0 )
begin
insert into @InputTable
select A.a, A.b from @CC A
inner join @InputTable B
on A.b = B.a
where A.a not in( select a from @InputTable )
end
delete @InputTable where a in( select b from @InputTable )
declare a_Cursor CURSOR for select a from @InputTable order by a
open a_Cursor
declare @Int int, @Info varchar(100)
set @Info = ''
fetch next from a_Cursor into @Int
while @@fetch_status = 0
begin
if( @Info != '' ) set @Info = @Info + '+'
set @Info = @Info + cast( @Int as varchar(10) )
fetch next from a_Cursor into @Int
end
close a_Cursor
deallocate a_Cursor
print @Info
set nocount offTop




