求一存储过程!!急!!!

x1234521 2006-03-14 04:28:43
部门表结构:
dptid prentdptid ...
1 0
2 1
3 1
4 3
5 4
6 0

说明:dptid为部门ID,prentdptid为是否有父部门,0为没有(也就是顶级部门)。

人员表结构:
userid dptid ....
1 1
2 5
3 2
4 3

说明:userid 为用户ID

要求通过一个部门ID找出所有属于这个部门以及子部门的所有用户
注:部门级数不定。

请高手帮帮忙!!谢谢!
...全文
95 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
x1234521 2006-03-15
  • 打赏
  • 举报
回复
楼上的高手!!!
谢谢!

结帖给分了!!
wgsasd311 2006-03-14
  • 打赏
  • 举报
回复
create table t1(dptid int, prentdptid int)
insert into t1
select 1,0 union all
select 2,1 union all
select 3, 1 union all
select 4, 3 union all
select 5, 4 union all
select 6, 0

create table t2(userid int, dptid int)
insert into t2
select 1, 1 union all
select 2, 5 union all
select 3, 2 union all
select 4, 3
go
/*要求通过一个部门ID找出所有属于这个部门以及子部门的所有用户
注:部门级数不 */

create proc p1(@dptid int)
as
declare @i int
declare @tb table (dptid int,parentid int,level int)
set @i=0
insert into @tb
select dptid,prentdptid,@i from t1 where dptid=@dptid
while @@rowcount>0
begin
set @i=@i+1
insert into @tb
select a.dptid,a.prentdptid,@i
from t1 a,@tb b
where a.prentdptid=b.dptid and b.level=@i-1
end
select b.userid
from @tb a,t2 b
where a.dptid=b.dptid
go


exec p1 3

drop table t1,t2
drop proc p1
xiaobr 2006-03-14
  • 打赏
  • 举报
回复
顶了。见好人,就顶
viptiger 2006-03-14
  • 打赏
  • 举报
回复
create procedure xxxx
@dptid int
as
select * from 人员表
where dptid in
(
select dptid from 部门表 where dptid = 1
union
select dptid from 部门表 where prentdptid = 1)

34,590

社区成员

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

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