| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 835 人关注过本帖
标题:SQL用 存储过程做下面的内容
只看楼主 加入收藏
yujietim
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2008-7-9
收藏
 问题点数:0 回复次数:3 
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



高手帮帮忙~!

[[it] 本帖最后由 yujietim 于 2008-8-5 16:07 编辑 [/it]]
搜索更多相关主题的帖子: SQL staff 结构 运行 
2008-08-05 11:22
yujietim
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2008-7-9
收藏
得分:0 
。。怎么没人额- - #
2008-08-05 14:55
bb3852
Rank: 1
等 级:新手上路
帖 子:82
专家分:0
注 册:2008-4-21
收藏
得分:0 
-----建表
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-08-06 09:45
bb3852
Rank: 1
等 级:新手上路
帖 子:82
专家分:0
注 册:2008-4-21
收藏
得分:0 
-----测试,执行存储过程allid即可

http://hi.baidu.com/bb3852
数据库开发有兴趣的朋友们,里面有很多原创代码
qq:184882759
2008-08-06 09:46
快速回复:SQL用 存储过程做下面的内容
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011546 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved