SQL递归函数列出父级的所有子级(ID ParentID模式)

  1. view plain copy to clipboard print ?
    1. --调用方法:   
    2. --select * from GetChild('24')   
    3. --select id from GetChild('24')   
    4. --select * from KuCun where ProductType in(select id from GetChild('24'))   
    5.   
    6. Create function [dbo].[GetChild](@ID varchar(10))   
    7. returns @t table(ID varchar(10),ParentID varchar(10),Level int)   
    8. as  
    9. begin  
    10.     declare @i int  
    11.     set @i = 1   
    12.     insert into @t select @ID,@ID,0 --当前级,本级,如果不要的话可以注释掉或再加个参数来选择操作   
    13.     insert into @t select ID,ParentID,@i from Dept where ParentID = @ID   
    14.   
    15.     while @@rowcount<>0   
    16.     begin  
    17.         set @i = @i + 1   
    18.         insert into @t   
    19.         select  
    20.             a.ID,a.ParentID,@i   
    21.         from  
    22.             Dept a,@t b   
    23.         where  
    24.             a.ParentID=b.ID and b.Level = @i-1   
    25.     end  
    26.     return  
    27. end  
    28.   
    29. --------------------------------------------------------------------------------   
    30. --在SQL Server2005中其实提供了CTE[公共表表达式]来实现递归:    
    31. Declare @Id Int    
    32. Set @Id = 24;    ---在此修改父节点    
    33.   
    34. With RootNodeCTE(Id,ParentId)    
    35. As    
    36. (    
    37. Select Id,ParentId From Dept Where ParentId In (@Id)    
    38. Union All    
    39. Select Dept.Id,Dept.ParentId From RootNodeCTE    
    40. Inner Join Dept   
    41. On RootNodeCTE.Id = Dept.ParentId    
    42. )    
    43.   
    44. Select * From RootNodeCTE   
    45.   
    46. --------------------------------------------------------------------------------   
    47. --------------------------------------------------------------------------------   
    48. --------------------------------------------------------------------------------   
    49.   
    50. --生成测试数据   
    51. create table Dept(ID int,ParentID int,msg varchar(20))   
    52. insert into Dept select 1,0,null  
    53. insert into Dept select 2,1,null  
    54. insert into Dept select 3,1,null  
    55. insert into Dept select 4,2,null  
    56. insert into Dept select 5,3,null  
    57. insert into Dept select 6,5,null  
    58. insert into Dept select 7,6,null  
    59. go   
    60.   
    61.   
    62. --创建用户定义函数   
    63. Create function [dbo].[GetChild](@ID varchar(10))   
    64. returns @t table(ID varchar(10),ParentID varchar(10),Level int)   
    65. as  
    66. begin  
    67.     declare @i int  
    68.     set @i = 1   
    69.     insert into @t select @ID,@ID,0 --当前级,本级,如果不要的话可以注释掉或再加个参数来选择操作   
    70.     insert into @t select ID,ParentID,@i from Dept where ParentID = @ID   
    71.   
    72.     while @@rowcount<>0   
    73.     begin  
    74.         set @i = @i + 1   
    75.         insert into @t   
    76.         select  
    77.             a.ID,a.ParentID,@i   
    78.         from  
    79.             Dept a,@t b   
    80.         where  
    81.             a.ParentID=b.ID and b.Level = @i-1   
    82.     end  
    83.     return  
    84. end  
    85.   
    86.   
    87. --执行查询   
    88. select ID from dbo.GetChild(3)   
    89. go   
    90.   
    91. --输出结果   
    92. /*   
    93. 3   
    94. 5   
    95. 6   
    96. 7   
    97. */   
    98.   
    99. --删除测试数据   
    100. drop function GetChild   
    101. drop table Dept   
    102.   
    103. --http://topic.csdn.net/u/20080409/16/1fb7d941-b1a1-4326-a936-230ddf057cbe.html  
    --调用方法:
    --select * from GetChild('24')
    --select id from GetChild('24')
    --select * from KuCun where ProductType in(select id from GetChild('24'))
    
    Create 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 ID,ParentID,@i from Dept where ParentID = @ID
    
        while @@rowcount<>0
        begin
            set @i = @i + 1
            insert into @t
            select
                a.ID,a.ParentID,@i
            from
                Dept a,@t b
            where
                a.ParentID=b.ID and b.Level = @i-1
        end
        return
    end
    
    --------------------------------------------------------------------------------
    --在SQL Server2005中其实提供了CTE[公共表表达式]来实现递归: 
    Declare @Id Int 
    Set @Id = 24;    ---在此修改父节点 
    
    With RootNodeCTE(Id,ParentId) 
    As 
    ( 
    Select Id,ParentId From Dept Where ParentId In (@Id) 
    Union All 
    Select Dept.Id,Dept.ParentId From RootNodeCTE 
    Inner Join Dept
    On RootNodeCTE.Id = Dept.ParentId 
    ) 
    
    Select * From RootNodeCTE
    
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    
    --生成测试数据
    create table Dept(ID int,ParentID int,msg varchar(20))
    insert into Dept select 1,0,null
    insert into Dept select 2,1,null
    insert into Dept select 3,1,null
    insert into Dept select 4,2,null
    insert into Dept select 5,3,null
    insert into Dept select 6,5,null
    insert into Dept select 7,6,null
    go
    
    
    --创建用户定义函数
    Create 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 ID,ParentID,@i from Dept where ParentID = @ID
    
        while @@rowcount<>0
        begin
            set @i = @i + 1
            insert into @t
            select
                a.ID,a.ParentID,@i
            from
                Dept a,@t b
            where
                a.ParentID=b.ID and b.Level = @i-1
        end
        return
    end
    
    
    --执行查询
    select ID from dbo.GetChild(3)
    go
    
    --输出结果
    /*
    3
    5
    6
    7
    */
    
    --删除测试数据
    drop function GetChild
    drop table Dept
    
    
    首页  >  数据库  >   SQL Server  > 正文
    怎么实现获取指定ID下的所有子级(sql2005)
  2. 怎么实现获取指定ID下的所有子级(sql2005)
     
    Sql代码  
      www.2cto.com  
    create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10))  
    insert into tb values('001' , null , N'广东省')  
    insert into tb values('002' , '001' , N'广州市')  
    insert into tb values('003' , '001' , N'深圳市')  
    insert into tb values('004' , '002' , N'天河区')  
    insert into tb values('005' , '003' , N'罗湖区')  
    insert into tb values('006' , '003' , N'福田区')  
    insert into tb values('007' , '003' , N'宝安区')  
    insert into tb values('008' , '007' , N'西乡镇')  
    insert into tb values('009' , '007' , N'龙华镇')  
    insert into tb values('010' , '007' , N'松岗镇')  
    go  
      
    DECLARE @ID VARCHAR(3)  
      
    --查询ID = '001'的所有子节点    www.2cto.com  
    SET @ID = '001'  
    ;WITH T AS  
    (  
    SELECT ID , PID , NAME   
    FROM TB  
    WHERE ID = @ID  
    UNION ALL  
    SELECT A.ID , A.PID , A.NAME   
    FROM TB AS A JOIN T AS B ON A.PID = B.ID  
    )  
    SELECT * FROM T ORDER BY 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 行受影响)  
    */  
      
    --查询ID = '002'的所有子节点  
    SET @ID = '002'  
    ;WITH T AS  
    (  
    SELECT ID , PID , NAME   
    FROM TB  
    WHERE ID = @ID  
    UNION ALL  
    SELECT A.ID , A.PID , A.NAME   
    FROM TB AS A JOIN T AS B ON A.PID = B.ID  
    )  
    SELECT * FROM T ORDER BY ID  
    /*  
    ID PID NAME  
    ---- ---- ----------  
    002 001 广州市  
    004 002 天河区  
      
    (2 行受影响)  
    */  
      
    --查询ID = '003'的所有子节点  
    SET @ID = '003'  
    ;WITH T AS  
    (  
    SELECT ID , PID , NAME   
    FROM TB  
    WHERE ID = @ID  
    UNION ALL  
    SELECT A.ID , A.PID , A.NAME   
    FROM TB AS A JOIN T AS B ON A.PID = B.ID  
    )  
    SELECT * FROM T ORDER BY ID  
    /*  
    ID PID NAME  
    ---- ---- ----------  
    003 001 深圳市  
    005 003 罗湖区  
    006 003 福田区  
    007 003 宝安区  
    008 007 西乡镇  
    009 007 龙华镇  
    010 007 松岗镇  
      
    (7 行受影响)  
    */  
      
    drop table tb  
      
    --注:除ID值不一样外,三个SQL语句是一样的。  

你可能感兴趣的:(SQL递归函数列出父级的所有子级(ID ParentID模式))