请教,在SQL里的递归问题

pp1234567890 2003-08-22 10:46:07
我最近碰见一个问题,在SQL里能不能写递归程序???如果能写,该怎么写,是象写一般的SQL语句,还是写存储过程???哪里有例子
...全文
52 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
happydreamer 2003-08-22
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1375/1375432.xml?temp=.1264917

example

存储过程的递归调用

求:5!


create proc p10 (@in int, @out int output) as
declare @o1 int
declare @i1 int
if @in <= 1
set @out = 1
else
begin
set @i1 = @in - 1
exec sp_executesql N'exec p10 @i1, @o1 output',
N'@i1 int, @o1 int output',
@i1, @o1 output

set @out = @in * @o1
end
return

declare @o1 int
exec p10 5, @o1 output
select @o1
hjb111 2003-08-22
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1375/1375432.xml?temp=8.771914E-02
hjb111 2003-08-22
  • 打赏
  • 举报
回复
declare @temp table (id int)

insert @temp values (1)

while exists (
select col3 from 表
where col1 in (select id from @temp)
and col3 not in (select id from @temp)
)
insert @temp
select col3 from 表
where col1 in (select id from @temp)
and col3 not in (select id from @temp)
---把所有子节点插入@temp

select * from 表
where col3 in (select col3 from @temp)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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