有这样一些基础数据:
user_group_id  per_group_id  user_group_name 
1             0                  总管理员
2             1                  一级管理员
3             2                  二级管理员
4             3                  三级管理员
8             4                  四级管理员
10             8                  五级管理员
...现在有个需求,就是总管理员可以查看下面所有的管理员组,一级管理员可以查看下面所有的组,不能查看上一级的。
以此类推,每一级的管理员只能查看同级或者是下一级的,如何实现比较好呢?
管理员登陆的时候已经记录下了user_group_id。用user_group_id>=?  这个条件不行。我想还是写一个遍历语句。不知道思路该是怎么样的。也可以在C#环境下写算法。只要能实现均可。谢谢!

解决方案 »

  1.   

    --测试数据
    if OBJECT_ID('tb') is not null 
    drop table tb 
    go
    CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
    INSERT tb SELECT '001',NULL ,'山东省'
    UNION ALL SELECT '002','001','烟台市'
    UNION ALL SELECT '004','002','招远市'
    UNION ALL SELECT '003','001','青岛市'
    UNION ALL SELECT '005',NULL ,'四会市'
    UNION ALL SELECT '006','005','清远市'
    UNION ALL SELECT '007','006','小分市'
    GO
    --2000的方法--查询指定节点及其所有子节点的函数
    CREATE FUNCTION f_Cid(@ID char(3))
    RETURNS @t_Level TABLE(ID char(3),Level int)
    AS
    BEGIN
    declare @Level int
    set @level=1
    insert @t_level select @id,@level
    while @@rowcount>0
    begin 
        set @level=@level+1
        insert @t_Level select tb.id,@level
         from tb join @t_level t on tb.pid=t.id
         where t.level+1=@level
       end
       return
    endselect tb.*
    from tb join dbo.f_cid('002') b
    on tb.ID=b.id
    /*
    ID   PID  Name
    ---- ---- ----------
    002  001  烟台市
    004  002  招远市*/
    go
    --2005的方法(CTE)declare @n varchar(10)
    set @n='002'
    ;with 
    jidian as
    (
      select * from tb where ID=@n
      union all
      select t.* from jidian j join tb t on j.ID=t.PID
    )
    select * from jidian 
    go
    /*
    ID   PID  Name
    ---- ---- ----------
    002  001  烟台市
    004  002  招远市
    */
      

  2.   

    -- 建立演示环境
    CREATE TABLE Dept(
    id int PRIMARY KEY, 
    parent_id int,
    name nvarchar(20))
    INSERT Dept
    SELECT 0, 0, N'<全部>' UNION ALL
    SELECT 1, 0, N'财务部' UNION ALL
    SELECT 2, 0, N'行政部' UNION ALL
    SELECT 3, 0, N'业务部' UNION ALL
    SELECT 4, 0, N'业务部' UNION ALL
    SELECT 5, 4, N'销售部' UNION ALL
    SELECT 6, 4, N'MIS' UNION ALL
    SELECT 7, 6, N'UI' UNION ALL
    SELECT 8, 6, N'软件开发' UNION ALL
    SELECT 9, 8, N'内部开发'
    GO-- 查询指定部门下面的所有部门
    DECLARE @Dept_name nvarchar(20)
    SET @Dept_name = N'MIS'
    ;WITH
    DEPTS AS(
    -- 定位点成员
    SELECT * FROM Dept
    WHERE name = @Dept_name
    UNION ALL
    -- 递归成员, 通过引用CTE自身与Dept基表JOIN实现递归
    SELECT A.*
    FROM Dept A, DEPTS B
    WHERE A.parent_id = B.id
    )
    SELECT * FROM DEPTS
    GO-- 删除演示环境
    DROP TABLE Dept
      

  3.   

    看清楚 不能访问上一层 所以BOM的先就不贴了 免得被笑话
      

  4.   


    --测试数据
    if OBJECT_ID('tb') is not null 
    drop table tb 
    go
    CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
    INSERT tb SELECT '001',NULL ,'山东省'
    UNION ALL SELECT '002','001','烟台市'
    UNION ALL SELECT '004','002','招远市'
    UNION ALL SELECT '003','001','青岛市'
    UNION ALL SELECT '005',NULL ,'四会市'
    UNION ALL SELECT '006','005','清远市'
    UNION ALL SELECT '007','006','小分市'
    GO
    --2000的方法--查询指定节点及其所有子节点的函数
    CREATE FUNCTION f_Cid(@ID char(3))
    RETURNS @t_Level TABLE(ID char(3),Level int)
    AS
    BEGIN
    declare @Level int
    set @level=1
    insert @t_level select @id,@level
    while @@rowcount>0
    begin 
        set @level=@level+1
        insert @t_Level select tb.id,@level
         from tb join @t_level t on tb.pid=t.id
         where t.level+1=@level
       end
       return
    endselect tb.*
    from tb join dbo.f_cid('002') b
    on tb.ID=b.id
    /*
    ID   PID  Name
    ---- ---- ----------
    002  001  烟台市
    004  002  招远市*/
    go
    --2005的方法(CTE)declare @n varchar(10)
    set @n='002'
    ;with 
    jidian as
    (
      select * from tb where ID=@n
      union all
      select t.* from jidian j join tb t on j.ID=t.PID
    )
    select * from jidian 
    go
    /*
    ID   PID  Name
    ---- ---- ----------
    002  001  烟台市
    004  002  招远市
    */
      

  5.   

    这是树型
    --树型结构处理之双编号(节点复制与删除)
    if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0 
      drop table tb
    create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000))
    insert tb
    select '0001',null,'云南省'
    union all select '0002','0001','昆明市'
    union all select '0003','0001','昭通市'
    union all select '0009','0001','大理市'
    union all select '0008',null,'四川省'
    union all select '0004',null,'贵州省'
    union all select '0005','0002','五华区'
    union all select '0007','0003','水富县'
    union all select '0006','0005','西园路192号'
    union all select '0010','0006','金色梧桐3d-702'
    union all select '0011','0010','科时空科技有限公司'
    union all select '0015','0007','两碗乡'
    union all select '0013','0015','两碗村'
    union all select '0012','0013','某跨国集团董事长'
    union all select '0014','0008','成都市'
    go
    delete tb where ybh='0005'--节点删除(常用有两种方法(1,查找节点和所有的子节点,2,使用触发器)
    create trigger 
      dbo.tb_delete
    on tb
    for delete
    as
    --如果没有满足删除条件的记录,则直接返回
    if @@rowcount=0
      return
    --select * from tb
    declare @tt table(ybh varchar(100),level int)
    declare @level int
    set @level=0
    select * from deleted
    insert @tt(ybh,level)
    select a.ybh,@level 
      from tb a,deleted b
      where a.ebh=b.ybhwhile @@ROWCOUNT>0
    begin
      set @level=@level+1
        insert @tt(ybh,level)
          select a.ybh,@level
           from tb a,@tt b
           where a.ebh=b.ybh and b.level=@level-1
    end
    delete a from tb a,@tt b where a.ybh=b.ybh
    go
    --移动节点及所有的子节点(只要改变父编号所属即可)--         select * from tb
    --复制节点及所有的子节点
    declare @bb table(ybh nvarchar(100),nbh nvarchar(100),ebh nvarchar(100),beizhu nvarchar(100))
    declare @source_code nvarchar(100),
            @destination_code nvarchar(100)
    set @source_code='0002'
    set @destination_code='0008'declare @newcount intselect @newcount=COUNT(*)+1 from tbinsert @bb(ybh,nbh,ebh,beizhu)
     select cast(right(power(10,4)+@newcount,4) as varchar),ybh,ybh,beizhu from tb where ybh=@source_codewhile @@rowcount>0
       begin
         set @newcount=@newcount+1
         insert @bb(ybh,nbh,ebh,beizhu)
           select cast(right(power(10,4)+@newcount,4) as varchar),a.ybh,b.ybh,a.beizhu
             from tb a,@bb b
             where a.ebh=b.nbh and b.ybh=@newcount-1
       end
    update @bb set ebh=@destination_code where nbh=@source_codeinsert tb
    select ybh,ebh,beizhu from @bb
    不行楼主自己试试吧!结果就不贴了!
      

  6.   


    -- 建立演示环境
    CREATE TABLE Dept(
        id int PRIMARY KEY, 
        parent_id int,
        name nvarchar(20))
    INSERT Dept
    SELECT 0, 0, N'<全部>' UNION ALL
    SELECT 1, 0, N'财务部' UNION ALL
    SELECT 2, 0, N'行政部' UNION ALL
    SELECT 3, 0, N'业务部' UNION ALL
    SELECT 4, 0, N'业务部' UNION ALL
    SELECT 5, 4, N'销售部' UNION ALL
    SELECT 6, 4, N'MIS' UNION ALL
    SELECT 7, 6, N'UI' UNION ALL
    SELECT 8, 6, N'软件开发' UNION ALL
    SELECT 9, 8, N'内部开发'
    GO-- 查询指定部门下面的所有部门
    DECLARE @Dept_name nvarchar(20)
    SET @Dept_name = N'MIS'
    ;WITH
    DEPTS AS(
        -- 定位点成员
        SELECT * FROM Dept
        WHERE name = @Dept_name
        UNION ALL
        -- 递归成员, 通过引用CTE自身与Dept基表JOIN实现递归
        SELECT A.*
        FROM Dept A, DEPTS B
        WHERE A.parent_id = B.id
    )
    SELECT * FROM DEPTS
    GO-- 删除演示环境
    DROP TABLE Dept
      

  7.   

    --> 测试数据:@tb
    declare @tb table([user_group_id] int,[per_group_id] int,[user_group_name] varchar(10))
    insert @tb
    select 1,0,'总管理员' union all
    select 2,1,'一级管理员' union all
    select 3,2,'二级管理员' union all
    select 4,3,'三级管理员' union all
    select 8,4,'四级管理员' union all
    select 10,8,'五级管理员'
    --2005;with t as(
    select * from @tb where [user_group_name]='三级管理员'
    union all
    select a.* from @tb a  ,t where a.[per_group_id]=t.[user_group_id])
    select * from t
    /*
    user_group_id per_group_id user_group_name
    ------------- ------------ ---------------
    4             3            三级管理员
    8             4            四级管理员
    10            8            五级管理员(3 行受影响)
    */
      

  8.   

    /*
    Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)   Jul  9 2008 14:43:34   Copyright (c) 
    1988-2008 Microsoft Corporation  Enterprise Evaluation Edition on Windows NT 5.1 <X86> 
    (Build 2600: Service Pack 3) 
     愿和大家共同进步
    如有雷同、实属巧合
    ●●●●●2009-09-03 17:47:36.077●●●●●
     ★★★★★soft_wsx★★★★★
    */
    --树型结构处理之双编号(广度深度排序)
    if OBJECTPROPERTY(object_id('tb'),'isusertable')<>0 
      drop table tb
    create table tb(ybh nvarchar(10),ebh nvarchar(10),beizhu nvarchar(1000))
    insert tb
    select '0001',null,'云南省'
    union all select '0002','0001','昆明市'
    union all select '0003','0001','昭通市'
    union all select '0009','0001','大理市'
    union all select '0008',null,'四川省'
    union all select '0004',null,'贵州省'
    union all select '0005','0002','五华区'
    union all select '0007','0002','水富县'
    union all select '0006','0005','西园路192号'
    union all select '0010','0006','金色梧桐'
    union all select '0011','0010','科技有限公司'
    union all select '0015','0007','两碗乡'
    union all select '0013','0015','两碗村'
    union all select '0012','0013','某跨国集团董事长'
    union all select '0014','0008','成都市'--select * from tb
    --广度排序(先显示第一层节点,再显示第二次节点......)
    --定义辅助表
    declare @level_tb table(bh nvarchar(10),level int)
    declare @level int
    set @level=0
    insert @level_tb(bh,level)
    select ybh,@level from tb where ebh is null
    while @@ROWCOUNT>0
      begin
        set @level=@level+1
        insert @level_tb(bh,level)
          select ybh,@level 
            from tb a,@level_tb b
            where a.ebh=b.bh 
                  and b.level=@level-1
      end 
      select a.*,b.* from tb a,@level_tb b where a.ybh=b.bh order by level
    /*
    ybh ebh beizhu bh level
    0001 NULL 云南省 0001 0
    0008 NULL 四川省 0008 0
    0004 NULL 贵州省 0004 0
    0002 0001 昆明市 0002 1
    0003 0001 昭通市 0003 1
    0009 0001 大理市 0009 1
    0014 0008 成都市 0014 1
    0005 0002 五华区 0005 2
    0007 0002 水富县 0007 2
    0006 0005 西园路192号 0006 3
    0015 0007 两碗乡 0015 3
    0010 0006 金色梧桐 0010 4
    0013 0015 两碗村 0013 4
    0011 0010 科技有限公司 0011 5
    0012 0013 某跨国集团董事长 0012 5
    */
      
      --深度排序(模拟单编码法)
       declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
      declare @level int
      set @level=0
      insert @level_tt(ybh,ebh,level)
      select ybh,ybh,@level from tb where ebh is null
      while @@ROWCOUNT>0
      begin 
              set @level=@level+1
              insert @level_tt(ybh,ebh,level)
              select a.ybh,b.ebh+a.ybh,@level
                from tb a,@level_tt b
                where a.ebh=b.ybh and b.level=@level-1
     end
    select space(b.level*2)+'----'+a.beizhu,a.*,b.*
      from tb a,@level_tt b
      where a.ybh=b.ybh
      order by b.ebh
    /*(无列名) ybh ebh beizhu ybh ebh level
    ----云南省 0001 NULL 云南省 0001 0001 0
      ----昆明市 0002 0001 昆明市 0002 00010002 1
        ----五华区 0005 0002 五华区 0005 000100020005 2
          ----西园路192号 0006 0005 西园路192号 0006 0001000200050006 3
            ----金色梧桐 0010 0006 金色梧桐 0010 00010002000500060010 4
              ----科技有限公司 0011 0010 科技有限公司 0011 000100020005000600100011 5
        ----水富县 0007 0002 水富县 0007 000100020007 2
          ----两碗乡 0015 0007 两碗乡 0015 0001000200070015 3
            ----两碗村 0013 0015 两碗村 0013 00010002000700150013 4
              ----某跨国集团董事长 0012 0013 某跨国集团董事长 0012 000100020007001500130012 5
      ----昭通市 0003 0001 昭通市 0003 00010003 1
      ----大理市 0009 0001 大理市 0009 00010009 1
    ----贵州省 0004 NULL 贵州省 0004 0004 0
    ----四川省 0008 NULL 四川省 0008 0008 0
      ----成都市 0014 0008 成都市 0014 00080014 1
      */
      
      
      
      --查找子节点(包括本身节点和子节点)
     declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
      declare @level int
      set @level=0
      insert @level_tt(ybh,ebh,level)
      select ybh,ybh,@level from tb where ybh='0005'
      while @@ROWCOUNT>0
      begin 
              set @level=@level+1
              insert @level_tt(ybh,ebh,level)
              select a.ybh,b.ebh+a.ybh,@level
                from tb a,@level_tt b
                where a.ebh=b.ybh and b.level=@level-1
     end
    select space(b.level*2)+'----'+a.beizhu,a.*,b.*
      from tb a,@level_tt b
      where a.ybh=b.ybh
      order by b.ebh
      
     /*
     (无列名) ybh ebh beizhu ybh ebh level
    ----五华区 0005 0002 五华区 0005 0005 0
      ----西园路192号 0006 0005 西园路192号 0006 00050006 1
        ----金色梧桐 0010 0006 金色梧桐 0010 000500060010 2
          ----科技有限公司 0011 0010 科技有限公司 0011 0005000600100011 3
          */
      
      --查的父节点(包括本身节点和所有的你节点)
     declare @level_tt table(ybh nvarchar(1000),ebh nvarchar(1000),level int)
      declare @level int
      set @level=0
      insert @level_tt(ybh,ebh,level)
      select ybh,ebh,@level from tb where ebh='0005'
      while @@ROWCOUNT>0
      begin 
              set @level=@level+1
              insert @level_tt(ybh,ebh,level)
              select a.ebh,b.ebh+a.ebh,@level
                from tb a,@level_tt b
                where a.ybh=b.ybh and b.level=@level-1
     end
    select space(b.level*2)+'----'+a.beizhu,a.*,b.*
      from tb a,@level_tt b
      where a.ybh=b.ybh
      order by b.ebh desc
     
     /*
     (无列名) ybh ebh beizhu ybh ebh level
          ----云南省 0001 NULL 云南省 0001 0005000500020001 3
        ----昆明市 0002 0001 昆明市 0002 000500050002 2
      ----五华区 0005 0002 五华区 0005 00050005 1
    ----西园路192号 0006 0005 西园路192号 0006 0005 0
    */
      正确的!
      

  9.   

    我觉得,对你的这个需求:
    完全可以用这句实现select * from tb where user_group_id >= @id --其他的,看bom结构的语法,上面贴了一大堆
      

  10.   


    我已经在上面写了,用user_group_id>=?  这个条件不行。
    貌似12楼的算法可以借鉴。谢谢大家!准备结贴了。
    人人有分!
      

  11.   

    --> 生成测试数据表:tbIf not object_id('[tb]') is null
    Drop table [tb]
    Go
    Create table [tb]([user_group_id] int,[per_group_id] int,[user_group_name] nvarchar(5))
    Insert [tb]
    Select 1,0,N'总管理员' union all
    Select 2,1,N'一级管理员' union all
    Select 3,2,N'二级管理员' union all
    Select 4,3,N'三级管理员' union all
    Select 8,4,N'四级管理员' union all
    Select 10,8,N'五级管理员'
    Go
    --Select * from [tb]-->SQL2005查询如下:;with t as
    (
    select * from tb where [user_group_name]='二级管理员'
    union all
    select a.* from tb a join t b on a.[per_group_id]=b.[user_group_id]
    )
    select * from t
    /*
    user_group_id per_group_id user_group_name
    3 2 二级管理员
    4 3 三级管理员
    8 4 四级管理员
    10 8 五级管理员
    */以二级管理员为例.
      

  12.   

    哦,不好意思,跟12楼重了.写个2K的.--> 生成测试数据表:tbIf not object_id('[tb]') is null
    Drop table [tb]
    Go
    Create table [tb]([user_group_id] int,[per_group_id] int,[user_group_name] nvarchar(5))
    Insert [tb]
    Select 1,0,N'总管理员' union all
    Select 2,1,N'一级管理员' union all
    Select 3,2,N'二级管理员' union all
    Select 4,3,N'三级管理员' union all
    Select 8,4,N'四级管理员' union all
    Select 10,8,N'五级管理员'
    Go
    --Select * from [tb]-->SQL2000查询如下:
    declare @t table([user_group_id] int,[per_group_id] int,[user_group_name] nvarchar(5),lvl int)
    declare @i int
    set @i=0
    insert @t select *,@i from tb where [user_group_name]='二级管理员'
    while @@rowcount>0
    begin
    set @i=@i+1
    insert @t select a.*,@i from tb a join @t b on a.[per_group_id]=b.[user_group_id] and b.lvl=@i-1
    endselect * from @t
    /*
    user_group_id per_group_id user_group_name
    3 2 二级管理员
    4 3 三级管理员
    8 4 四级管理员
    10 8 五级管理员
    */