| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦   
共有 330 人关注过本帖
标题:SQL用 存储过程做下面的内容
收藏  订阅  推荐  打印
yujietim
Rank: 1
等级:新手上路
帖子:29
积分:438
注册:2008-7-9
SQL用 存储过程做下面的内容

有一表staff   如下:
id   managerid
1    null
2     1
3     2
4     1
5     4
6     2
7     6
8     1
9     1


当运行存储过程 得到一新表 结构如下
ID   Path
1    1
2    1,2
3    1,2,3
4    1,4
5    1,4,5
6    1,2,6
7    1,2,6,7
8    1,8
9    1,9



高手帮帮忙~!

[ 本帖最后由 yujietim 于 2008-8-5 16:07 编辑 ]
2008-8-5 11:22
yujietim
Rank: 1
等级:新手上路
帖子:29
积分:438
注册:2008-7-9

。。怎么没人额- - #
2008-8-5 14:55
bb3852
Rank: 2
等级:注册会员
帖子:82
积分:1040
注册:2008-4-21

-----建表
create table staff(id int,managerid int)
create table newtable(ID int,[Path] varchar(100))
----------所有id
create proc allid
as
begin

declare @nid int
declare cursor_id cursor for select id from staff
open cursor_id
fetch next from cursor_id into @nid
while(@@fetch_status=0)
begin

exec ppp @nid,@nid

fetch next from cursor_id into @nid
end
close cursor_id
deallocate  cursro_id

end



----------------单个id执行
create proc ppp
(@id int,@child_id int)
as
begin
  declare @managerid int
  select @managerid=managerid from staff where id=@child_id
  
  if not exists(select 1 from newtable where ID=@id)
  begin
  select 1
  insert into newtable(ID,[Path])
  values(@id,cast(@id as varchar))
  end

  if(@managerid is null)
  select 2
  else
  begin
  select 3
  update newtable set [Path]=[Path]+','+cast(@managerid as varchar) where ID=@id
  exec ppp @id,@managerid
  end
end

http://hi.baidu.com/bb3852
数据库开发有兴趣的朋友们,里面有很多原创代码
qq:184882759
2008-8-6 09:45
bb3852
Rank: 2
等级:注册会员
帖子:82
积分:1040
注册:2008-4-21

-----测试,执行存储过程allid即可

http://hi.baidu.com/bb3852
数据库开发有兴趣的朋友们,里面有很多原创代码
qq:184882759
2008-8-6 09:46
共有 329 人关注过本帖
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.075906 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved