SqlServer 树结构数据 子节点父节点的查询
2014-09-04 15:20
316 查看
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
*/
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中查询表结构(表主键 、列说明、列数据类型、所有表名)的Sql语句
- 数据结构(第二天)单链表的基本操作,创建单链表,头插法,尾插法,删除节点,查询节点
- 答:SQLServer DBA 三十问之一: char、varchar、nvarchar之间的区别(包括用途和空间占用);xml类型查找某个节点的数据有哪些方法,哪个效率高;使用存储 过程和使用T-SQL查询数据有啥不一样;
- SQLServer实现树型结构数据查询的存储过程,类似oracle的connect by
- oracle查询出树形结构的表,父节点数据是子节点的数据和
- sqlserver中一个表中树形结构递归数据查询
- 7. Orcle树形结构(类似数据字典有父子类关系),查询末节点的所有记录
- SQLServer中查询表结构(表主键 、列说明、列数据类型、所有表名)的Sql语句
- sqlserver查询指定树形结构的所有子节点
- sqlserver查询指定树形结构的所有子节点
- SqlServer找出所有的子节点,并按树形结构、层次查询
- SQLServer中查询表结构(表主键 、列说明、列数据类型、所有表名)的Sql语句
- ORACLE 树形结构数据 查询某结点下全部子节点无限递归的前2个数据
- 在sqlplus下从oracle数据库中 查询表结构 的数据 并输出的 过程
- sqlserver中查询access的数据
- SQLServer采用分页的方式查询数据
- 查询不同结构两表 字段数据不一样
- 【数据操作】查看SqlServer数据表结构
- 转 SqlServer的查询通知功能 从sqlserver层面上来解决界面数据的即时更新
- 基于数据字典的通用查询系统(二)数据库组成结构的分析