码迷,mamicode.com
首页 > 数据库 > 详细

SqlServer 树结构数据 子节点父节点的查询

时间:2014-09-04 16:51:49      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:io   ar   数据   sp   on   c   sql   table   har   


declare @t table (C_ID int,PcName varchar(6),ParentID int)
insert into @t
select 1,‘安徽‘,0 union all
select 2,‘安庆‘,1 union all
select 3,‘安庆市‘,2 union all
select 4,‘怀宁县‘,2 union all
select 5,‘潜山县‘,2 union all
select 6,‘宿松县‘,2 union all
select 7,‘太湖县‘,2 union all
select 8,‘桐城市‘,2 union all
select 9,‘望江县‘,2 union all
select 10,‘岳西县‘,2 union all
select 11,‘枞阳县‘,2

;with maco as
(
select * from @t where c_id=11
union all
select t.* from @t t,maco m where t.C_ID=m.ParentID
)
select * from maco order by c_id

/*
C_ID PcName ParentID
----------- ------ -----------
1 安徽 0
2 安庆 1
11 枞阳县 2
*/

 

SqlServer 树结构数据 子节点父节点的查询

标签:io   ar   数据   sp   on   c   sql   table   har   

原文地址:http://www.cnblogs.com/chengchuang/p/3956184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!