火焰

valeb
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

sqlserver 循环 + 递归 修改 末节点 标识

Posted on 2023-08-29 19:15  valeb  阅读(9)  评论(0编辑  收藏  举报
DECLARE @cnt INT = 0;
WHILE @cnt < 27
BEGIN
  SET @cnt = @cnt + 1;
   PRINT @cnt;
  
 with temp (id,[Name],ParentCategriesID)as 
 (
 select id,[Name],ParentCategriesID from Categories  where id=27  
  union all 
 select a.id,a.[Name],a.ParentCategriesID from Categories a 
 inner join temp on a.ParentCategriesID = temp.[id] 
 ),
 lastNode as( 
 select id from temp where id not in (select ParentCategriesID from Categories  ) 
 ) 
 update Categories set HasNext=0 where id in (select * from lastNode)
    
END