SQL Server 公用表表達式(CTE)實現(xiàn)遞歸的方法
公用表表達式簡介:
公用表表達式 (CTE) 可以認為是在單個 SELECT、INSERT、UPDATE、DELETE 或 CREATE VIEW 語句的執(zhí)行范圍內(nèi)定義的臨時結(jié)果集。CTE 與派生表類似,具體表現(xiàn)在不存儲為對象,并且只在查詢期間有效。與派生表的不同之處在于,公用表表達式 (CTE) 具有一個重要的優(yōu)點,那就是能夠引用其自身,從而創(chuàng)建遞歸 CTE。遞歸 CTE 是一個重復(fù)執(zhí)行初始 CTE 以返回數(shù)據(jù)子集直到獲取完整結(jié)果集的公用表表達式。
下面先創(chuàng)建一個表,并插入一些數(shù)據(jù):
create table Role_CTE ( Id int not null, Name nvarchar(32) not null, ParentId int not null ) insert into Role_CTE(Id,Name,ParentId) select '1','超級管理員','0' union select '2','管理員A','1' union select '3','管理員B','2' union select '4','會員AA','2' union select '5','會員AB','2' union select '6','會員BA','3' union select '7','會員BB','3' union select '8','用戶AAA','4' union select '9','用戶BBA','7' -- 創(chuàng)建一個復(fù)合聚集索引 create clustered index Clu_Role_CTE_Index on Role_CTE(Id,ParentId) with ( pad_index=on, fillfactor=50, drop_existing=off, statistics_norecompute=on ) select * from Role_CTE
查找指定節(jié)點的所有子孫節(jié)點:
使用普通 sql 語句實現(xiàn):
declare @level int declare @node int declare @ResTab table ( node int not null, lv int not null ) set @level=0 -- 表示初始的等級 set @node=3 --表示初始的節(jié)點ID,即從指定的哪個節(jié)點開始查找 insert into @ResTab -- 為表變量插入初始的數(shù)據(jù) select Id,@level from Role_CTE where Id=@node while(@@ROWCOUNT>0) begin set @level=@level+1 insert into @ResTab select b.Id,@level from @ResTab a join Role_CTE b on a.node=b.ParentId and lv=@level-1 -- join 等于 inner join(內(nèi)連接)和自連接 end select a.node,b.Name,a.lv from @ResTab a left join Role_CTE b on a.node=b.Id
以上是根據(jù)指定節(jié)點ID(3),查找父節(jié)點ID(即字段 ParentId)等于指定的節(jié)點ID,如果有就插入,并繼續(xù)循環(huán)。
PS:lv=@level-1 是重點,不然會進入死循環(huán),作用就是限制只插入一次。
如果需要限制循環(huán)的次數(shù),即遞歸的層數(shù),那么只需要在 while 條件里面添加一個限制即可。如下:
declare @level int declare @node int declare @num int declare @ResTab table ( node int not null, lv int not null ) set @level=0 -- 表示初始的等級 set @node=3 --表示初始的節(jié)點ID,即從指定的哪個節(jié)點開始查找 set @num=1 -- 指定遞歸層級,即循環(huán)的次數(shù) insert into @ResTab -- 為表變量插入初始的數(shù)據(jù) select Id,@level from Role_CTE where Id=@node while(@@ROWCOUNT>0 and @level<@num) begin set @level=@level+1 insert into @ResTab select b.Id,@level from @ResTab a join Role_CTE b on a.node=b.ParentId and lv=@level-1 -- join 等于 inner join(內(nèi)連接)和自連接 end select a.node,b.Name,a.lv from @ResTab a left join Role_CTE b on a.node=b.Id
當然,如果指定了循環(huán)次數(shù),就可以不用 while 判斷語句的 @@rowcount>0 了。
使用 SQL CTE 實現(xiàn):
declare @node int set @node=3; with temp_cte as ( select Id,Name,0 lv -- 查詢出“根節(jié)點”,即指定的起始節(jié)點 from Role_CTE where Id=@node union all select b.Id,b.Name,a.lv+1 from temp_cte a join Role_CTE b on a.Id=b.ParentId ) select * from temp_cte
使用 CTE 控制遞歸的層數(shù),與上面類似。如下:
declare @node int declare @num int set @node=3; set @num=1; with temp_cte as ( select Id,Name,0 lv -- 查詢出“根節(jié)點”,即指定的起始節(jié)點 from Role_CTE where Id=@node union all select b.Id,b.Name,a.lv+1 from temp_cte a join Role_CTE b on a.Id=b.ParentId and a.lv<@num --控制遞歸層數(shù) ) select * from temp_cte
查找指定節(jié)點的所有祖先節(jié)點:
使用普通 sql 語句實現(xiàn):
declare @level int declare @node int declare @num int declare @ResTab table ( node int not null, lv int not null ) set @level=0 -- 表示初始的等級 set @node=8 --表示初始的節(jié)點ID,即從指定的哪個節(jié)點開始查找 set @num=2 -- 指定遞歸層級,即循環(huán)的次數(shù) while(@level<=@num and @node is not null) -- 如果為空就表示沒有查到父級了 begin insert into @ResTab select @node,@level set @level=@level+1 select @node=ParentId from Role_CTE where Id=@node end select a.node,b.Name,a.lv from @ResTab a left join Role_CTE b on a.node=b.Id
使用 SQL CTE 實現(xiàn):
declare @node int declare @num int set @node=8; set @num=2; with temp_cte as ( select Id,Name,ParentId,0 lv -- 查詢出“根節(jié)點”,即指定的起始節(jié)點 from Role_CTE where Id=@node union all select b.Id,b.Name,b.ParentId,a.lv+1 from temp_cte a join Role_CTE b on a.ParentId=b.Id and a.lv < @num --控制遞歸層數(shù) ) select * from temp_cte
以上所述是小編給大家介紹的SQL Server 公用表表達式(CTE)實現(xiàn)遞歸的方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的,在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Mysql8公用表表達式CTE詳解
- MySQL8.0之CTE(公用表表達式)的使用
- SQL?Server使用T-SQL進階之公用表表達式(CTE)
- mysql8 公用表表達式CTE的使用方法實例分析
- sql server使用公用表表達式CTE通過遞歸方式編寫通用函數(shù)自動生成連續(xù)數(shù)字和日期
- SqlServer使用公用表表達式(CTE)實現(xiàn)無限級樹形構(gòu)建
- 關(guān)于SQL中CTE(公用表表達式)(Common Table Expression)的總結(jié)
- SQL2005 學(xué)習(xí)筆記 公用表表達式(CTE)
- SqlServer公用表表達式(CTE)的具體使用
相關(guān)文章
Sql Server 索引使用情況及優(yōu)化的相關(guān)Sql語句分享
Sql Server 索引使用情況及優(yōu)化的相關(guān) Sql 語句,非常好的SQL語句,記錄于此,需要的朋友可以參考下2012-05-05詳解SQL Server數(shù)據(jù)庫鏈接查詢的方式
本文我們主要介紹了SQL Server數(shù)據(jù)庫鏈接查詢的方式,包括內(nèi)連接、外連接和交叉連接等的內(nèi)容,需要的朋友可以參考下2015-08-08針對Sqlserver大數(shù)據(jù)量插入速度慢或丟失數(shù)據(jù)的解決方法
這篇文章主要介紹了針對Sqlserver大數(shù)據(jù)量插入速度慢或丟失數(shù)據(jù)的解決方法,很有實用價值,需要的朋友可以參考下2014-07-07sql server建表時設(shè)置ID字段自增的簡單方法
這篇文章主要介紹了 sql server建表時設(shè)置ID字段自增的簡單方法,需要的朋友可以參考下2017-10-10