首页 > 数据库技术 > 详细

SQL Server 递归

时间:2016-06-08 12:03:15      阅读:217      评论:0      收藏:0      [点我收藏+]

SQL Server 没有类似于Oracle START WITH NAME=‘xx‘ CONNECT BY PRIOR ID=PARENT_ID这样的语句,但是可以通过自定义标准函数+With语句实现,速度也是杠杠的

 

ALTER FUNCTION  [dbo].[RecursionSysLocation]
(    
    -- Add the parameters for the function here
    @ParentId nvarchar(36)
)
RETURNS TABLE 
AS
RETURN 
(
    with temp ( [Id], [parentid])
    as
    (
        select Id, ParentId
        from SysLocation
        where ParentId = @ParentId
        union all
        select a.Id, a.ParentId
        from SysLocation a
        inner join temp on a.ParentId = temp.[Id]        
    )
    
    select s.Id, s.ParentId from SysLocation s where Id=@ParentId
    union all
    select * from temp
)

 

SQL Server 递归

原文:http://www.cnblogs.com/lcy1984/p/5569369.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!