復制代碼 代碼如下:
--由父項遞歸下級
with cte(id,parentid,text)
as
(--父項
select id,parentid,text from treeview where parentid = 450
union all
--遞歸結果集中的下級
select t.id,t.parentid,t.text from treeview as t
inner join cte as c on t.parentid = c.id
)
select id,parentid,text from cte
---------------------
--由子級遞歸父項
with cte(id,parentid,text)
as
(--下級父項
select id,parentid,text from treeview where id = 450
union all
--遞歸結果集中的父項
select t.id,t.parentid,t.text from treeview as t
inner join cte as c on t.id = c.parentid
)
select id,parentid,text from cte
您可能感興趣的文章:- sql server遞歸子節(jié)點、父節(jié)點sql查詢表結構的實例
- SQL Server 樹形表非循環(huán)遞歸查詢的實例詳解
- 使用SqlServer CTE遞歸查詢處理樹、圖和層次結構
- 使用SQLSERVER 2005/2008 遞歸CTE查詢樹型結構的方法
- SQLSERVER2005 中樹形數據的遞歸查詢
- 高效的SQLSERVER分頁查詢(推薦)
- SQL Server SQL高級查詢語句小結
- Sql server2005 優(yōu)化查詢速度50個方法小結
- SQLserver 實現分組統(tǒng)計查詢(按月、小時分組)
- sqlserver 模糊查詢常用方法
- sql server實現遞歸查詢的方法示例