首页 > 数据库技术 > 详细

sql递归显示层级数据

时间:2017-11-17 16:52:24      阅读:329      评论:0      收藏:0      [点我收藏+]
;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

 

sql递归显示层级数据

原文:http://www.cnblogs.com/sxhlf/p/7852355.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!