SQL递归

with cte as 
(
    select ClassID,ClassName,ParentID,ParentPath,CAST(row_number() over(order by ClassID) as varbinary(max)) as sort
    from dc_SysItemsClass
    where ClassID = 1
    
    union all
    
    select a.ClassID,a.ClassName,a.ParentID,a.ParentPath,sort+CAST(row_number() over(partition by a.ParentID order by a.ClassID) as varbinary(max))
    from  dc_SysItemsClass a
    inner join cte b
    ON a.ParentID = b.ClassID 
)
select ClassID,ClassName,ParentID,ParentPath,sort
from  cte 
order by sort
原文地址:https://www.cnblogs.com/superfeeling/p/14334647.html