zoukankan      html  css  js  c++  java
  • 获取树形节根节点下面所有层级子节点

    --bom结构,查找节点下所有子节点:
    
    create table Tree(DomainID uniqueidentifier,ParentID uniqueidentifier,desn varchar(10))
    insert into Tree select '00000000-0000-0000-0000-000000000001','00000000-0000-0000-0000-000000000000','A1'
    insert into Tree select '00000000-0000-0000-0000-000000000002','00000000-0000-0000-0000-000000000001','A2'
    insert into Tree select '00000000-0000-0000-0000-000000000003','00000000-0000-0000-0000-000000000002','A3'
    insert into Tree select '00000000-0000-0000-0000-000000000004','00000000-0000-0000-0000-000000000003','A4'
    insert into Tree select '00000000-0000-0000-0000-000000000005','00000000-0000-0000-0000-000000000000','B1'
    insert into Tree select '00000000-0000-0000-0000-000000000006','00000000-0000-0000-0000-000000000005','B2'
    insert into Tree select '00000000-0000-0000-0000-000000000007','00000000-0000-0000-0000-000000000006','B3'
    insert into Tree select '00000000-0000-0000-0000-000000000008','00000000-0000-0000-0000-000000000007','C1'
    insert into Tree select '00000000-0000-0000-0000-000000000009','00000000-0000-0000-0000-000000000008','C2'
    
    select * from Tree
    
    --求某个节点下所有子节点:
    create function find_childid(@id uniqueidentifier)
    returns varchar(500)
    as
    begin
         declare @t table(DomainID uniqueidentifier,ParentID uniqueidentifier,desn varchar(10),lev int)
         declare @lev int
         set @lev=1
         insert into @t select *,@lev from  Tree where DomainID=@id
         while(@@rowcount>0)
         begin
              set @lev=@lev+1
              insert into @t select a.*,@lev from Tree a,@t b
              where a.ParentID=b.DomainID and b.lev=@lev-1
         end
         declare @cids varchar(500)
         select @cids=isnull(@cids+',','')+ltrim(DomainID) from @t order by lev
         return @cids
    end
    go
    
    --调用函数
    select * from Tree
    select *,ids=dbo.find_childid(DomainID) from Tree
    
    
    --得到每个节点路径:
    create proc GetNodeFullPath
    @id uniqueidentifier
    as
    select *,cast(' ' as varchar(10)) fullpath  into #Tree from Tree
    DECLARE @i int,@j int
    set @i=0
    set @j=1
    select @i='00000000-0000-0000-0000-000000000009' from #Tree
    update #Tree set fullpath=DomainID 
    while @j<=@i
    begin
           update #Tree set fullpath=a.fullpath+','+ltrim(#Tree.DomainID) 
                from #Tree inner join #Tree a on #Tree.ParentID=a.DomainID 
           where #Tree.ParentID=@j 
           set @j=@j+1
    end
    select * from #Tree
    go
    --调用存储过程
    exec GetNodeFullPath '00000000-0000-0000-0000-000000000009'
    --消息 245,级别 16,状态 1,过程 GetNodeFullPath,第 8 行
    --在将 varchar 值 '00000000-0000-0000-0000-000000000009' 转换成数据类型 int 时失败。
    

    最简方法:

    --向下获取SQL
    DECLARE @ID NVARCHAR(100)
    SET @ID = '00000000-0000-0000-0000-000000000032' --外部传入
    ;WITH DEPTS AS(
        SELECT * FROM Tree
        WHERE DomainID = @ID
        UNION ALL
        SELECT A.*
        FROM Tree A, DEPTS B
        WHERE A.ParentID = B.DomainID
    )
    SELECT * FROM DEPTS
    

     节点ID是INT类型的:

    View Code
    --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
  • 相关阅读:
    从 0 → 1,学习Linux该这么开始!
    Web和移动开发的未来
    css-div中文字过多(内容超出div宽度)后自动换行
    js+css--单选按钮,自定义选中的颜色???(性别按钮,男女)
    css-按钮中有图片和文字,怎么才能让文字和图片都中??
    js-点出弹框后(除了点击窗口上的叉子),点其他地方能够关闭窗口???
    css-外面元素的高度,由里面的元素进行撑开(由内部的高度决定)
    js-将传来的数据排序,让(全部)这个小按钮小圈圈,始终排列在最前面
    echart--如何在折线图上添加矩形背景(可以借用bar柱状图的实现效果)
    echart-如何将x轴和y轴的原点进行重合???
  • 原文地址:https://www.cnblogs.com/8090sns/p/2721339.html
Copyright © 2011-2022 走看看