递归求值,高手帮忙

qizhanfeng 2004-04-20 09:10:26
父 子 值
A B 1
B C 1
B D 2
D E 3
-------------------
A

B(1)
C(1) D(2)
E(3)
A有 1 个B,B有 2 个D,D有 3 个E
我想算出A有几个E,几个D ,几个B
怎么写递归,不用递归还有更好的方法吗
我在存储过程里定义游标,然后递归调用该存储过程
实现上面所说的,总说游标已打开或者不存在
怎么解决 ,谢谢
...全文
75 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
aderly 2004-09-09
  • 打赏
  • 举报
回复
做個記號學習
zjcxc 2004-04-20
  • 打赏
  • 举报
回复
这样解释就很明确了
zjcxc 2004-04-20
  • 打赏
  • 举报
回复
--测试

--测试数据
create table 表(父 char(1),子 char(1),值 int)
insert 表 select 'A','B',1
union all select 'B','C',1
union all select 'B','D',2
union all select 'D','E',3
go

--存储过程
create proc p_calc
@id char(1)='A', --要查询的节点
@num int=1 --数量
as
declare @l int
set @l=0
declare @re table(id char(1),level int,num int)
insert @re select @id,@l,@num
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.子,@l,num=b.num*a.值
from 表 a join @re b on a.父=b.id
where b.level=@l-1
end
select id,num=sum(num) from @re group by id
go

--调用查询
exec p_calc
go

--删除测试
drop table 表
drop proc p_calc

/*--测试结果
id num
---- -----------
A 1
B 1
C 1
D 2
E 6

(所影响的行数为 5 行)
--*/

qizhanfeng 2004-04-20
  • 打赏
  • 举报
回复
就是
表的意思
A 需要 B 1 个
B 需要 D 2 个
D 需要 E 3 个

我想知道
A需要几个 B,几个D,几个E
结果是
A需要 1个B
A需要 ((1个B)*2)=2 个D
A需要 ((2个D)*3)=6 个E
zjcxc 2004-04-20
  • 打赏
  • 举报
回复
看不明白你的意思.
qizhanfeng 2004-04-20
  • 打赏
  • 举报
回复
可能我没说清楚
应该是这样的结果
A 1
B 1
C 1
D 2
E 6
谢谢
zjcxc 2004-04-20
  • 打赏
  • 举报
回复
--测试

--测试数据
create table 表(父 char(1),子 char(1),值 int)
insert 表 select 'A','B',1
union all select 'B','C',1
union all select 'B','D',2
union all select 'D','E',3
go

--存储过程
create proc p_calc
as
declare @l int
set @l=0
declare @re table(id char(1),level int,[count] int)
insert @re select 子,@l,0
from 表 a
where not exists(select 1 from 表 where 父=a.子)
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.父,@l,[count]=b.[count]+1
from 表 a join @re b on a.子=b.id
where b.level=@l-1
end
select id,[count]=sum([count]) from @re group by id
go

--调用查询
exec p_calc

go

--删除测试
drop table 表
drop proc p_calc

/*--测试结果
id count
---- -----------
A 5
B 3
C 0
D 1
E 0

(所影响的行数为 5 行)
--*/
qizhanfeng 2004-04-20
  • 打赏
  • 举报
回复
能不能用存储过程,不用function我用的是sqlserver7.0没有函数
qizhanfeng 2004-04-20
  • 打赏
  • 举报
回复
E的结果好像不对,应该是 6 呀
zjcxc 2004-04-20
  • 打赏
  • 举报
回复
--测试

--测试数据
create table 表(父 char(1),子 char(1),值 int)
insert 表 select 'A','B',1
union all select 'B','C',1
union all select 'B','D',2
union all select 'D','E',3
go

--自定义函数
create function f_id(@id char(1))
returns @re table(id char(1),level int)
as
begin
declare @l int
set @l=0
insert @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.子,@l
from 表 a join @re b on a.父=b.id
where b.level=@l-1
end
return
end
go

--调用,查询A下面的
select * from f_id('A')

go

--删除测试
drop table 表
drop function f_id

/*--测试结果
id level
---- -----------
A 0
B 1
C 2
D 2
E 3

(所影响的行数为 5 行)
--*/
zjcxc 2004-04-20
  • 打赏
  • 举报
回复
--自定义函数
create function f_id(@id char(1))
returns @re table(id char(1),level int)
as
begin
declare @l int
set @l=0
insert @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.子,@l
from 表 a join @re b on a.父=b.id
where b.level=@l-1
end
return
end
go

--调用,查询A下面的
select * from f_id('A')
qizhanfeng 2004-04-20
  • 打赏
  • 举报
回复
谢谢了
我不用表变量了用实际存在的表

zjcxc 2004-04-20
  • 打赏
  • 举报
回复
--存储过程
create proc p_calc
@id char(1)='A', --要查询的节点
@num int=1 --数量
as
declare @l int
set @l=0
create table #re(id char(1),level int,num int)
insert #re select @id,@l,@num
while @@rowcount>0
begin
set @l=@l+1
insert #re select a.子,@l,num=b.num*a.值
from 表 a join #re b on a.父=b.id
where b.level=@l-1
end
select id,num=sum(num) from #re group by id
go
qizhanfeng 2004-04-20
  • 打赏
  • 举报
回复
declare @re table(id char(1),level int,num int)
在sqlserver7.0下能执行吗
Incorrect syntax near the keyword 'table'报这个错误
这是为什么

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧