復(fù)制代碼 代碼如下:
--由父項(xiàng)遞歸下級(jí)
with cte(id,parentid,text)
as
(--父項(xiàng)
select id,parentid,text from treeview where parentid = 450
union all
--遞歸結(jié)果集中的下級(jí)
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
---------------------
--由子級(jí)遞歸父項(xiàng)
with cte(id,parentid,text)
as
(--下級(jí)父項(xiàng)
select id,parentid,text from treeview where id = 450
union all
--遞歸結(jié)果集中的父項(xiàng)
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é)點(diǎn)、父節(jié)點(diǎn)sql查詢(xún)表結(jié)構(gòu)的實(shí)例
- SQL Server 樹(shù)形表非循環(huán)遞歸查詢(xún)的實(shí)例詳解
- 使用SqlServer CTE遞歸查詢(xún)處理樹(shù)、圖和層次結(jié)構(gòu)
- 使用SQLSERVER 2005/2008 遞歸CTE查詢(xún)樹(shù)型結(jié)構(gòu)的方法
- SQLSERVER2005 中樹(shù)形數(shù)據(jù)的遞歸查詢(xún)
- 高效的SQLSERVER分頁(yè)查詢(xún)(推薦)
- SQL Server SQL高級(jí)查詢(xún)語(yǔ)句小結(jié)
- Sql server2005 優(yōu)化查詢(xún)速度50個(gè)方法小結(jié)
- SQLserver 實(shí)現(xiàn)分組統(tǒng)計(jì)查詢(xún)(按月、小時(shí)分組)
- sqlserver 模糊查詢(xún)常用方法
- sql server實(shí)現(xiàn)遞歸查詢(xún)的方法示例