sql递归显示层级数据


;with catChild as(select *,cast(right('00000'+cast(Sort as varchar),5) as varchar(max)) as ssort 
from Category where ID = '123'
    union all select t.*,
    cast(ssort+right('00000'+cast(t.Sort as varchar),5) as varchar(max))
     from catChild, Category  t
    where catChild.ID = t.ParentID)
select * from catChild where IsVolumes='1'  order by ssort,Sort


;with cte as
 (select ID,Name,ParentID,1 level,
         cast(right('00000'+cast(Sort as varchar),5) as varchar(max)) as ssort
    from Category where ID = '111A' 
  union all select t.ID,t.Name,t.ParentID,c.level + 1,
       cast(ssort+right('00000'+cast(t.Sort as varchar),5) as varchar(max))
    from Category t
    join cte c on t.ParentID = c.ID)
select ID, Name, ParentID, level,ssort
  from cte order by ssort

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM