树的结构:
tree(id,name,parent)
已知某个节点的ID,要获取其下所有级别的子类的ID,用SQL语句或者函数实现都可以。

解决方案 »

  1.   

    create table tb(id varchar(3) , pid varchar(3) , name varchar(10)) 
    insert into tb values('001' , null  , '广东省') 
    insert into tb values('002' , '001' , '广州市') 
    insert into tb values('003' , '001' , '深圳市') 
    insert into tb values('004' , '002' , '天河区') 
    insert into tb values('005' , '003' , '罗湖区') 
    insert into tb values('006' , '003' , '福田区') 
    insert into tb values('007' , '003' , '宝安区') 
    insert into tb values('008' , '007' , '西乡镇') 
    insert into tb values('009' , '007' , '龙华镇') 
    insert into tb values('010' , '007' , '松岗镇') 
    go --查询指定节点及其所有子节点的函数 
    create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int) 
    as 
    begin 
      declare @level int 
      set @level = 1 
      insert into @t_level select @id , @level 
      while @@ROWCOUNT > 0 
      begin 
        set @level = @level + 1 
        insert into @t_level select a.id , @level 
        from tb a , @t_Level b 
        where a.pid = b.id and b.level = @level - 1 
      end 
      return 
    end 
    go --调用函数查询001(广东省)及其所有子节点 
    select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id 
    /* 
    id  pid  name      
    ---- ---- ---------- 
    001  NULL 广东省 
    002  001  广州市 
    003  001  深圳市 
    004  002  天河区 
    005  003  罗湖区 
    006  003  福田区 
    007  003  宝安区 
    008  007  西乡镇 
    009  007  龙华镇 
    010  007  松岗镇 (所影响的行数为 10 行) 
    */ --调用函数查询002(广州市)及其所有子节点 
    select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id 
    /* 
    id  pid  name      
    ---- ---- ---------- 
    002  001  广州市 
    004  002  天河区 (所影响的行数为 2 行) 
    */ --调用函数查询003(深圳市)及其所有子节点 
    select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id 
    /* 
    id  pid  name      
    ---- ---- ---------- 
    003  001  深圳市 
    005  003  罗湖区 
    006  003  福田区 
    007  003  宝安区 
    008  007  西乡镇 
    009  007  龙华镇 
    010  007  松岗镇 (所影响的行数为 7 行) 
    */ drop table tb 
    drop function f_cid--bom结构,查找节点下所有子节点:create table os(id int,parentid int,desn varchar(10))
    insert into os select 1,0,'体育用品'
    insert into os select 2,0,'户外运动'
    insert into os select 3,1,'篮球'
    insert into os select 4,1,'足球'
    insert into os select 5,2,'帐篷'
    insert into os select 6,2,'登山鞋'
    insert into os select 7,0,'男士用品'
    insert into os select 8,7,'刮胡刀'
    insert into os select 9,3,'大号篮球'--求个节点下所有子节点:
    create function f_cid(@id int)
    returns varchar(500)
    as
    begin
         declare @t table(id int,parentid int,desn varchar(10),lev int)
         declare @lev int
         set @lev=1
         insert into @t select *,@lev from  os where id=@id
         while(@@rowcount>0)
         begin
              set @lev=@lev+1
              insert into @t select a.*,@lev from os a,@t b
              where a.parentid=b.id and b.lev=@lev-1
         end
         declare @cids varchar(500)
         select @cids=isnull(@cids+',','')+ltrim(id) from @t order by lev
         return @cids
    end
    go--调用函数
    select *,ids=dbo.f_cid(id) from os
    --得到每个节点路径:
    create proc wsp2
    @id int
    as
    select *,cast(' ' as varchar(10)) fullpath  into #os from os
    DECLARE @i int,@j int
    set @i=0
    set @j=1
    select @i=max(parentid) from #os
    update #os set fullpath=id 
    while @j<=@i
    begin
           update #os set fullpath=a.fullpath+','+ltrim(#os.id) 
                from #os inner join #os a on #os.parentid=a.id 
           where #os.parentid=@j 
           set @j=@j+1
    end
    select * from #os
    go
    --调用存储过程
    exec wsp2 1
      

  2.   

    -->Title:Generating test data
    -->Author:wufeng4552
    -->Date :2009-09-30 08:52:38
    set nocount on
    if object_id('tb','U')is not null drop table tb
    go
    create table tb(ID int, ParentID int)
    insert into tb select 1,0  
    insert into tb select 2,1  
    insert into tb select 3,1  
    insert into tb select 4,2  
    insert into tb select 5,3  
    insert into tb select 6,5  
    insert into tb select 7,6
    -->Title:查找指定節點下的子結點
    if object_id('Uf_GetChildID')is not null drop function Uf_GetChildID
    go
    create function Uf_GetChildID(@ParentID int)
    returns @t table(ID int)
    as
    begin
       insert @t select ID from tb where ParentID=@ParentID
       while @@rowcount<>0
       begin
          insert @t select a.ID from tb a inner join @t b
          on a.ParentID=b.id and 
          not exists(select 1 from @t where id=a.id)
       end 
    return
    end
    go
    select * from dbo.Uf_GetChildID(5)
    /*
    ID
    -----------
    6
    7
    */
    -->Title:查找指定節點的所有父結點
    if object_id('Uf_GetParentID')is not null drop function Uf_GetParentID
    go
    create function Uf_GetParentID(@ID int)
    returns @t table(ParentID int)
    as
    begin
       insert @t select ParentID from tb where ID=@ID
       while @@rowcount!=0
       begin
         insert @t select a.ParentID from tb a inner join @t b
           on a.id=b.ParentID and 
           not exists(select 1 from @t where ParentID=a.ParentID)
       end
      return
    end
    go
    select * from dbo.Uf_GetParentID(2)
    /*
    ParentID
    -----------
    1
    0
    */
      

  3.   

    ---2005
    USE tempdb
    GO-- 建立演示环境
    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
      

  4.   


    /*
    标题:查询指定节点及其所有子节点的函数
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有子节点的函数
    create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @id , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.id , @level
        from tb a , @t_Level b
        where a.pid = b.id and b.level = @level - 1
      end
      return
    end
    go--调用函数查询001(广东省)及其所有子节点
    select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市
    003  001  深圳市
    004  002  天河区
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 10 行)
    */--调用函数查询002(广州市)及其所有子节点
    select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    002  001  广州市
    004  002  天河区(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有子节点
    select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    003  001  深圳市
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 7 行)
    */drop table tb
    drop function f_cid@@ROWCOUNT:返回受上一语句影响的行数。
    返回类型:integer。
    注释:任何不返回行的语句将这一变量设置为 0 ,如 IF 语句。
    示例:下面的示例执行 UPDATE 语句并用 @@ROWCOUNT 来检测是否有发生更改的行。UPDATE authors SET au_lname = 'Jones' WHERE au_id = '999-888-7777'
    IF @@ROWCOUNT = 0
       print 'Warning: No rows were updated'结果:(所影响的行数为 0 行)
    Warning: No rows were updated
      

  5.   


    create table tb(parent varchar(100),child varchar(100))
    go
    insert into tb
    select 'world','europe' union
    select 'world','north america' union
    select 'europe','france' union
    select 'france','paris' union
    select 'north america','united states' union
    select 'north america','canada' union
    select 'united states','new york' union
    select 'united states','washington' union
    select 'new york','new york city' union
    select 'washington','redmond'
                        
    --以树形方式展开层次结构
    create proc show_tree 
    @node varchar(100)
    as
    begin
    set nocount on
    create table #stack(node varchar(100),level int)
    declare @level int--
    declare @currentnode varchar(100)
    --初始化堆栈
    insert into #stack values(@node,1)set @level=1
    while @level>0
    begin
    if exists(select * from #stack where level=@level)--第@level层是否还有没有处理的节点
    begin
    select @currentnode=node from #stack where level=@level--从第@level层取出一个节点,当前层可能有多个节点
                                                                           --该语句取出其中的一个                
    print space(@level)+@currentnode--打印当前节点
    insert into #stack--当前节点子节点进栈
    select child,@level+1 from tb where parent=@currentnode 
    delete from #stack where node=@currentnode and level=@level--删除当前节点
                    if @@rowcount>0 
    set @level=@level+1
                    
    end
    else

    set @level=@level-1

    end
    end/*
    如果当前级别 (@level) 的堆栈中有任何项目,该过程将选择其中一个,并称之为 @current。
    缩进项目 @level 空格,然后打印该项目。
    从堆栈中删除该项目以免重复处理它,然后将其所有子项目添加到堆栈的下一级 (@level + 1) 中。这是唯一使用层次表 (#stack) 的地方。 
    如果使用传统的编程语言,就必须找到每个子项目并将其逐个添加到堆栈中。而使用 Transact-SQL,只用一个语句就能找到并添加所有的子项目,以免又使用一个嵌套循环。如果有子项目 (IF @@ROWCOUNT > 0),则下降一级处理它们 (@level = @level + 1);否则,继续在当前级别上处理。
    最后,如果在当前级别的堆栈中没有待处理的项目,则返回到上一级,看上一级是否有待处理的项目 (@level = @level - 1)。当再没有上一级时,则展开完毕。 */show_tree 'world'结果:
     world
      north america
       united states
        washington
         redmond
        new york
         new york city
       canada
      europe
       france
        paris