zoukankan      html  css  js  c++  java
  • sql父子表结构,常用脚本

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

    数据准备

    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('惠州',2)
    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 dbo.Region AS R

     1.查询父节点的所有子节点

    /*
     * summary:递归获取所有子节点
    */
    CREATE 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(3)

    --CTE(公用表表达式)实现
    declare @id int
    set @id = 3
    ;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

    2.根据子节点追溯根节点

    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(8)
    go

     

    3.根据导航数据查询节点

    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

  • 相关阅读:
    概率dp呜呜
    树上路径问题---点分治题集
    POJ 1741 Tree
    接下来的计划
    很逗 的网络赛历程
    Codeforces Round #264 (Div. 2) D
    似乎了解了树形dp/
    先胡乱搭一个blog
    POJ 1330 (LCA)
    文件操作
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/10724863.html
Copyright © 2011-2022 走看看