Create Table TEST
(a Int,
 b Varchar(100),
 c Int)
Insert TEST Select 1,'aa',0
Union All Select 2,'bb',1
Union All Select 3,'cc',2
Union All Select 4,'dd',3
Union All Select 5,'ee',4
GO
--得到所有的子節點
Create Function GetChild(@b Varchar(100))
Returns @Child Table(a Int, b Varchar(100), c Int)
As
Begin
Insert @Child Select * From TEST Where c In (Select a From TEST Where b=@b)
While @@ROWCOUNT>0
Insert @Child Select B.* From @Child A Inner Join TEST B On A.a=B.c And B.c Not In (Select Distinct c From @Child)
Return
End
GO
Select * From dbo.GetChild('bb')
--Select * From dbo.GetChild('aa')
GO
Drop Table TEST
Drop Function GetChild
GO
--Result
/*
a b c
3 cc 2
4 dd 3
5 ee 4
*/

解决方案 »

  1.   

    加上一個得到所有父節點的Create Table TEST
    (a Int,
     b Varchar(100),
     c Int)
    Insert TEST Select 1,'aa',0
    Union All Select 2,'bb',1
    Union All Select 3,'cc',2
    Union All Select 4,'dd',3
    Union All Select 5,'ee',4
    GO
    --得到所有的子節點
    Create Function GetChild(@b Varchar(100))
    Returns @Child Table(a Int, b Varchar(100), c Int)
    As
    Begin
    Insert @Child Select * From TEST Where c In (Select a From TEST Where b=@b)
    While @@ROWCOUNT>0
    Insert @Child Select B.* From @Child A Inner Join TEST B On A.a=B.c And B.c Not In (Select Distinct c From @Child)
    Return
    End
    GO
    --得到所有的父節點
    Create Function GetParent(@b Varchar(100))
    Returns @Child Table(a Int, b Varchar(100), c Int)
    As
    Begin
    Insert @Child Select * From TEST Where a In (Select c From TEST Where b=@b)
    While @@ROWCOUNT>0
    Insert @Child Select B.* From @Child A Inner Join TEST B On A.c=B.a And B.a Not In (Select Distinct a From @Child)
    Return
    End
    GO
    Select * From dbo.GetChild('bb')
    --Select * From dbo.GetChild('aa')
    Select * From dbo.GetParent('cc')
    GO
    Drop Table TEST
    Drop Function GetChild,GetParent
    GO
    --Result
    /*
    a b c
    3 cc 2
    4 dd 3
    5 ee 4a b c
    2 bb 1
    1 aa 0
    */
      

  2.   

    修改下Create Table TEST
    (a Int,
     b Varchar(100),
     c Int)
    Insert TEST Select 1,'aa',0
    Union All Select 2,'bb',1
    Union All Select 3,'cc',2
    Union All Select 4,'dd',3
    Union All Select 5,'ee',4
    GO
    --得到所有的子節點
    Create Function GetChild(@b Varchar(100))
    Returns @Child Table(a Int, b Varchar(100), c Int)
    As
    Begin
    Insert @Child Select * From TEST Where c In (Select a From TEST Where b=@b)
    While @@ROWCOUNT>0
    Insert @Child Select B.* From @Child A Inner Join TEST B On A.a=B.c And B.c Not In (Select Distinct c From @Child)
    Return
    End
    GO
    --得到所有的父節點
    Create Function GetParent(@b Varchar(100))
    Returns @Parent Table(a Int, b Varchar(100), c Int)
    As
    Begin
    Insert @Parent Select * From TEST Where a In (Select c From TEST Where b=@b)
    While @@ROWCOUNT>0
    Insert @Parent Select B.* From @Parent A Inner Join TEST B On A.c=B.a And B.a Not In (Select Distinct a From @Parent)
    Return
    End
    GO
    Select * From dbo.GetChild('bb')
    --Select * From dbo.GetChild('aa')
    Select * From dbo.GetParent('cc')
    GO
    Drop Table TEST
    Drop Function GetChild,GetParent
    GO
    --Result
    /*
    a b c
    3 cc 2
    4 dd 3
    5 ee 4a b c
    2 bb 1
    1 aa 0
    */
      

  3.   

    Create Table TEST
    (a Int,
     b Varchar(100),
     c Int)
    Insert TEST Select 1,'aa',0
    Union All Select 2,'bb',1
    Union All Select 3,'cc',2
    Union All Select 4,'dd',3
    Union All Select 5,'ee',4CREATE FUNCTION f_str(@ID int)
    RETURNS @t_Level TABLE(ID int,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 a.a,@Level
    FROM test a,@t_Level b
    WHERE a.c=b.ID
    AND b.Level=@Level-1
    END
    RETURN
    END
    GOSELECT a.*
    FROM test a,f_str(2) b
    WHERE a.a=b.ID and a.a<>2
      

  4.   

    在调用函数的时候,输入bb所对应的a列的值,也就是2