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

发布时间 2023-08-29 19:25:29作者: valeb
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