根据父节点 找出其所有子节点或根据子节点找出其所有父级节点

根据父节点 找出其所有子节点

/*
找出一个节点下的所有子节点
*/
alter function [dbo].[GetChild](@ID varchar(10))     
returns @t table(ID varchar(10),ParentID varchar(10),Level int)     
as    
begin    
    declare @i int    
    set @i = 1    
       
    insert into @t select @ID,@ID,0 --当前级,本级,如果不要的话可以注释掉或再加个参数来选择操作     
    insert into @t   
    select wbs_sid,Parent_ID,@i   
    from pln_wbs where Parent_ID = @ID     
    
    
    while @@rowcount<>0     
    begin    
        set @i = @i + 1     
        insert into @t     
        select    
            a.wbs_sid,a.Parent_ID,@i     
        from    
            pln_wbs a,@t b     
        where    
            a.Parent_ID=b.ID and b.Level = @i-1     
    end    
    return             
end 

  

---恢复内容结束---

原文地址:https://www.cnblogs.com/YanYongSong/p/4482608.html