在一个做一个大企业的人事管理 
这个大企业各级部门有几千个 
我构造了一个部门树 
结点为各个部门 
当点击一个部门结点时,显示部门及该部门以下所了子部门的人员 
我用一个递归获取部门及其下所有子部门的id 
各个id之间用逗号分隔 
然后在人员表中使用:
SELECT * FROM  VIEW_人员信息  WHERE 有效否=1 AND 单位id IN (1,2,3,4,5,6,7,8,9,10,11,12,4740,4741,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,4881)
点击某些子部门比较多的部门时出现这样的情况,这还是我删除了很多的情况下的样子 ,实际上在点总总公司时,里面就有四千多个id,当然,总部门我可以不加条件。但我看了还是会有其它的部门会有几千个子部门的情况,这样我用这个方法就没办法看到这个单位下的所有人员了,不知大家遇到 类似问题时有没有什么好办法

解决方案 »

  1.   

    with cte as
    (
      select * from table where 父部门 is null
      union all
      select * from table join cte on table.父部门 =  cte.部门 
    )
    select * from cte
      

  2.   

    建议建立一个表值函数(用于返回你要的部门下的小部门编号,类似你写的1,2,3,4,5,6...)
    create function get_org(@ORGID VARCHAR(10)) --你要查的部门编号
    RETURN table
    as 
    begin
    /*用递归将@ORGID下的部门递归;with tb as 
    ()
    */
    return tb 
    nd --表值函数返回的结果是:
    1
    2
    3
    4
    5
    将人员信息表语表值函数连接:select *from  VIEW_人员信息 jion get_org('总部ID') ON 表值函数.id=单位id
    WHERE 有效否=1  
    我们公司就是这样做的
      

  3.   

    1楼的没看懂,没用过 with as ,能不能写祥细点啊?
    2楼的好像太复杂,
    有没有哪位朋友再给个方案啊
      

  4.   

    那就写个递归函数吧、、、、(SQL2000中)
      

  5.   

    --> 测试数据:dept
    if object_id('dept') is not null drop table dept
    create table dept(dept_id int identity, dept_name varchar(10), parent_dept int)
    insert dept
    select 'dept1', 0 union all
    select 'dept2', 1 union all
    select 'dept3', 2 union all
    select 'dept4', 3
    go
    --> 测试数据:emp
    if object_id('emp') is not null drop table emp
    create table emp(emp_id int identity, dept_id int, name varchar(10))
    insert into emp
    select 1, 'a' union all
    select 2, 'b' union all
    select 2, 'c' union all
    select 3, 'd' union all
    select 3, 'e' union all
    select 3, 'f' union all
    select 4, 'g' union all
    select 4, 'h' union all
    select 4, 'i' union all
    select 4, 'j'
    go--> SQL2000下级部门函数
    if object_id('fn_dept') is not null drop function fn_dept
    go
    create function fn_dept (@dept_id int)
    returns @t table (dept_id int, dept_name varchar(10), depth int) as
    begin
    declare @depth int
    set @depth = 0
    insert @t select @dept_id, dept_name, @depth from dept where dept_id = @dept_id
    while @@rowcount > 0
    begin
    set @depth = @depth + 1
    insert @t select a.dept_id, a.dept_name, @depth from dept a join @t b on a.parent_dept = b.dept_id where b.depth = @depth - 1
    end
    return
    end
    go--> 测试dept3所有员工
    select * from emp e join dbo.fn_dept(3) d on e.dept_id = d.dept_id
    /*
    emp_id      dept_id     name       dept_id     dept_name  depth
    ----------- ----------- ---------- ----------- ---------- -----------
    4           3           d          3           dept3      0
    5           3           e          3           dept3      0
    6           3           f          3           dept3      0
    7           4           g          4           dept4      1
    8           4           h          4           dept4      1
    9           4           i          4           dept4      1
    10          4           j          4           dept4      1
    */--> 删除测试
    drop table dept, emp
    drop function fn_dept
      

  6.   

    树形表数据的处理总结 
    http://blog.csdn.net/xys_777/archive/2010/06/15/5672481.aspx
      

  7.   

    create table dept(dept_id int identity, dept_name varchar(10), parent_dept int)
    insert dept
    select 'dept1', 0 union all
    select 'dept2', 1 union all
    select 'dept3', 2 union all
    select 'dept4', 3create table emp(empid varchar(4),dept_id int)
    insert into emp
    select '0001',2 union all
    select '0002',2 union all
    select '0003',3 union all
    select '0004',4 union all
    select '0005',5 union all
    select '0006',4  
    create    FUNCTION [dbo].[Get_Org]
    (
    @OrgID varchar(50) --组织编号
    )
    RETURNS 
    @Org TABLE 
    (
    orgid [varchar] (50), --组织编号
    orgname [varchar] (30),  --组织名称
    si int
    )
    AS
    BEGINdeclare @LoopCounter INT
    select @LoopCounter=0 --保留当前组织本身
    insert into @Org(orgid,orgname,si)
    select dept_id,dept_name,0 from dept where dept_id=@OrgID while @@rowcount>0 
    begin
         select @LoopCounter=@LoopCounter+1
         insert into @Org(orgid,orgname,si )
         select dept_id,dept_name,@LoopCounter from dept join @Org on orgid=parent_dept 
         where  si=@LoopCounter-1
    end
    return
    ENDselect *from [Get_Org]('3')--获取组织编号为3的下级所有组织select empid from emp join [Get_Org]('3') on orgid=dept_id  --获取组织编号 为3 的所有人员
    以上希望能达到你的目的