SQL SERVER 2005 公用表表达式(CTE)处理递归(父子关系)
2009-11-22 22:17
399 查看
CTE 一个突出优点就是处理递归的时候简单明了,让人一看就懂。这个是sql server 2005的新特性。
用with来实现这样的功能:
先看一个例子
在这个例子中orgunitid=1的记录是orgunitid=2的父类。
用with就能很轻松的实现这样的递归
with prodgroup
as
(select orgunitid,[type],shortname,orgunitparent from organizationunit where type=0 and orgunitid=1
union all
select a.orgunitid,a.[type],a.shortname,a.orgunitparent from organizationunit a inner join prodgroup b
on a.orgunitparent=b.orgunitid
)
select * from prodgroup
这里实现是是从上向下查,也可以从下往上查,
用with来实现这样的功能:
先看一个例子
在这个例子中orgunitid=1的记录是orgunitid=2的父类。
用with就能很轻松的实现这样的递归
with prodgroup
as
(select orgunitid,[type],shortname,orgunitparent from organizationunit where type=0 and orgunitid=1
union all
select a.orgunitid,a.[type],a.shortname,a.orgunitparent from organizationunit a inner join prodgroup b
on a.orgunitparent=b.orgunitid
)
select * from prodgroup
这里实现是是从上向下查,也可以从下往上查,
相关文章推荐
- tsql 树形数据的处理(sqlserver 2005 cte特性 递归公用表表达式显示层次目录)
- SQL Server 2005中使用公用表表达式的递归查询(CTE是个好东西)
- 使用SQL SERVER 2005/2008 递归CTE查询树型结构
- SQL Server 2005新特性之使用with关键字解决递归父子关系
- [SQL Server 2005/2008]递归更新update(使用CTE公用表达式)
- SQL Server 2005新特性之使用with关键字解决递归父子关系
- SQL Server 2005新特性之使用with关键字解决递归父子关系
- SQL Server 公用表表达式(CTE)实现递归
- SQL Server 2005新特性之使用with关键字解决递归父子关系
- Sql Server 2005+中用公用表表达式(CTE)实现递归查询
- SQL Server中公用表表达式 CTE 递归的生成帮助数据,以及递归的典型应用
- sqlserver 2005 公用表表达式CTE与 ORACLE递归生成树查询 对比
- SQL Server 公用表表达式(CTE)实现递归
- SQL Server 2005新特性之使用with关键字解决递归父子关系
- 利用SQL SERVER 2005 中的CTE 实现树的递归
- SQL Server 2005新特性之使用with关键字解决递归父子关系(详细)
- SQL Server 公用表表达式(CTE)实现递归的方法
- SQL Server 2005参考:CTE 公用表表达式
- SQL Server 2005中使用公用表表达式的递归查询(CTE是个好东西)
- SQL Server 2005---使用with关键字解决递归父子关系