子父节点查找

 
--1.cte版本,限制100级递归  
if object_id(N'getroot') > 0  
    drop function  getroot   
GO   
create function getroot 
    ( 
      @nootID int , 
      @isroot int --0为找子节点,其他为找父节点        
        
    ) 
returns table 
as      
return 
    with    troot ( id, ParentId, cityname ) 
              as ( select   id , 
                            ParentId , 
                            cityname 
                   from     yxs_provinces a 
                   where    case @isroot 
                              when 0 then a.ParentId 
                              else a.id 
                            end = @nootID 
                   union all 
                   select   ac.id , 
                            ac.ParentId , 
                            ac.cityname 
                   from     yxs_provinces ac 
                            inner join troot b on case @isroot 
                                            when 0 
                                            then ac.ParentId 
                                            else ac.id 
                                            end = case @isroot 
                                            when 0 then b.id 
                                            else b.ParentId 
                                            end 
                 ) 
    select  * 
    from    troot 
    where   id != @nootID 
      go   
       ------------------------------------------------------------   
       --找子节点  SELECT  *  FROM    dbo.getroot(260 , 0)   
       -------------------------------------------------------------   
       --找父节点  SELECT  *  FROM    dbo.getroot(260 , 1)   
       --------------------------------------------------------------   
       --2.非cte版本,无限制递归,效率稍微第一种方案低  
if object_id(N'getroot') > 0  
    drop function  getroot 
   GO   
create function Getroot 
    ( 
      @nootID int , 
      @isroot int--0为找子节点,其他为找父节点           
                   
        
    ) 
returns @t table 
    ( 
      id int , 
      ParentId int , 
      cityname varchar(20
    ) 
as  
    begin      
       --仔细观察可以发现注视中的代码和下面代码的差异     
       /*               INSERT  @t                  
       SELECT   id , ParentId , cityname 
       FROM     yxs_provinces a 
       WHERE    CASE @isroot 
                  WHEN 0 THEN a.ParentId 
                  ELSE a.id 
                END = @nootID 
       UNION ALL 
       SELECT   ac.id , ac.ParentId , ac.cityname 
       FROM     yxs_provinces ac 
       INNER JOIN @t b ON CASE @isroot 
                            WHEN 0 THEN ac.ParentId 
                            ELSE ac.id 
                          END = CASE @isroot 
                                  WHEN 0 THEN b.id 
                                  ELSE b.ParentId 
                                END 
       DELETE   @t 
       WHERE    id = @nootID              
           */                    
        insert  @t 
                select  id , 
                        ParentId , 
                        cityname 
                from    yxs_provinces a 
                where   case @isroot 
                          when 0 then a.ParentId 
                          else a.id 
                        end = @nootID 
        insert  @t 
                select  ac.id , 
                        ac.ParentId , 
                        ac.cityname 
                from    yxs_provinces ac 
                        inner join @t b on case @isroot 
                                            when 0 
                                            then ac.ParentId 
                                            else ac.id 
                                           end = case @isroot 
                                            when 0 then b.id 
                                            else b.ParentId 
                                            end 
        delete  @t 
        where   id = @nootID                
        return      
    end  
            go    
             --------------------------------------------------------------------   
             --找子节点   SELECT  *  FROM    dbo.getroot(260 , 0)     
             -------------------------------------------------------------     
             --找父节点    SELECT  *  FROM    dbo.getroot(260 , 1)     
posted @   qanholas  阅读(699)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示