zoukankan      html  css  js  c++  java
  • sql 函数实现三种父子递归

    在实际运用中经常会创建这样的结构表Category(Id, ParentId, Name),特别是用于树形结构时(菜单树,权限树..),这种表设计自然而然地会用到递归,若是在程序中进行递归(虽然在程序中递归真的更方便一些),无论是通过ADO.NET简单sql查找还是ORM属性关联都会执行多次sql语句,难免会造成一些性能上的损耗,所以干脆使用sql的函数来解决这个问题,用函数返回我们最终需要的结果。

    针对这类需求,这里我列出三种常用的递归:

    1. 以一个节点为基点,列出所有子节点直到无子 (找下级) 。这有点儿像点兵点将,主帅只有一个,下面是左将、右将,左将下面又有千夫长、百夫长,点兵时主帅下令集合,下面的将军只管各自的队伍。
    2. 以一个节点为基点,列出所有父节点直到祖先(找上级) 。
    3. 面包屑导航数据(单条数据)

    下面我以一幅图列出这三种形式(实线表现的是我们最终想要的数据,第三幅图中只有一条数据):

    1

    OK,现在让我们来实现这几个需求,step by step。

    1. 数据准备

    根据上面的图中的数据创建表结构和测试数据

    create table Region
    (
    Id int identity primary key,
    Name nvarchar(20),
    ParentId int 
    )
    go
    insert into Region(Name,ParentId) values('广东',NULL)
    insert into Region(Name,ParentId) values('深圳',1)
    insert into Region(Name,ParentId) values('惠州',1)
    insert into Region(Name,ParentId) values('罗湖区',2)
    insert into Region(Name,ParentId) values('福田区',2)
    insert into Region(Name,ParentId) values('龙岗区',2)
    insert into Region(Name,ParentId) values('惠阳区',3)
    insert into Region(Name,ParentId) values('龙门县',3)
    insert into Region(Name,ParentId) values('华强北',5)
    insert into Region(Name,ParentId) values('体育馆',5)
    
    select * from Region

    2. 正向递归实现

    /*
     * summary:递归获取所有子节点
    */
    alter function GetRecursiveChildren
    (
    @Id int
    )
    returns @t table(Id int,ParentId int,Name nvarchar(20), [Level] int)
    begin
        declare @i int
        set @i = 1
        --根节点,Level = 0
        insert into @t select @Id,@id,(select Name from Region where Id = @id),0
        --直属子节点,Level = 1
        insert into @t select Id,ParentId,Name,@i from Region where ParentId = @Id
        
        --如果没有新的值插入,循环结束
        while @@rowcount<>0
        begin
            set @i = @i + 1;
            insert into @t
            select 
                a.Id,a.ParentId,a.Name,@i
            from
                Region a, @t b
            where
                a.ParentId = b.Id and b.Level = @i - 1
        end
        return
    end
    go
    --调用函数
    select * from GetRecursiveChildren(2)

    执行上面的函数得到如下图的结果:

    image

    -----------------------------------------------------------------------------------------------------------------------------

    当然自sql 2005后微软提供了CTE(公用表表达式)也可以用于递归查询,请参阅使用公用表达式的递归查询

    上面的递归用CTE的sql代码如下:

    declare @id int
    set @id = 2
    ;with t as--如果CTE前面有语句,需要用分号隔断
    (
    select Id, ParentId, Name
    from Region
    where Id = @id
    union all
    select r1.Id,r1.ParentId,r1.Name
    from Region r1 join t as r2 on r1.ParentId = r2.Id
    )
    select * from t order by Id

    3. 逆向递归实现

    create function GetRecursiveParent
    (
    @Id int
    )
    returns @t table(Id int,ParentId int,Name nvarchar(20), [Level] int)
    as
    begin
        declare @i int
        set @i = 1
        --插入末节点,Level = 0
        insert into @t select @Id,@id,(select Name from Region where Id = @id),0
        --插入末节点的父节点,Level = 1
        insert into @t select Id,ParentId,Name,@i from Region 
        where Id = (select ParentId from Region where Id = @Id)
        --如果没有新的值插入,循环结束
        while @@rowcount<>0
        begin
            set @i = @i + 1;
            insert into @t
            select 
                a.Id,a.ParentId,a.Name,@i
            from
                Region a, @t b
            where
                a.Id = b.ParentId and b.Level = @i - 1
        end
        return    
    end
    go
    --调用函数
    select * from GetRecursiveParent(10)
    go

    执行这个函数得到的结果如下:

    image

    4. 面包屑实现

    create function GetLevel
    (
    @Id int
    )
    returns @level table(IdLevel varchar(100),NameLevel nvarchar(200))
    as
    begin
        declare @IdLevel varchar(100),@NameLevel nvarchar(200),@Name nvarchar(50)
        select @IdLevel = cast(@Id as varchar(10))
        select @NameLevel = (select Name from Region where Id = @Id)
        
        while(exists(select Id,ParentId from Region where Id = (select ParentId from Region where Id = @Id)))
        begin
            select @Id = Id,@Name = Name from Region where Id = (select ParentId from Region where Id = @Id)
            select @IdLevel = cast(@Id as varchar(10)) + '>' + @IdLevel
            select @NameLevel = @Name + '>' + @NameLevel
        end
        insert into @level select @IdLevel,@NameLevel
        return
    end
    go
    --调用函数
    select * from GetLevel(10)
    go

    调用这个函数的结果如下:

    image

  • 相关阅读:
    一个基于JBoss5.1+EJB3.0 登陆应用
    专题开发十二:JEECG微云高速开发平台-基础用户权限
    linux监控脚本
    centos Ddos防范开源软件使用及apache ab压测 测试
    centos curl web站点监控实践
    linux Qt5开发案例实践
    CentOS 基于KVM的云计算之虚拟化libvirt shell --- virsh
    linux shell命令行下操作mysql 删除mysql指定数据库下的所有表--亲测成功百分百测试通过--绝对可靠
    C指针
    Aix5~6小机运维
  • 原文地址:https://www.cnblogs.com/wichell/p/2338905.html
Copyright © 2011-2022 走看看