濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > SQL Server 公用表表達(dá)式(CTE)實(shí)現(xiàn)遞歸的方法

SQL Server 公用表表達(dá)式(CTE)實(shí)現(xiàn)遞歸的方法

熱門標(biāo)簽:武漢長沙外呼系統(tǒng)方法和技巧 智能語音外呼系統(tǒng)選哪家 怎樣在地圖上標(biāo)注路線圖標(biāo) 百度地圖標(biāo)注不同路線 京華物流公司地圖標(biāo)注 奧威地圖標(biāo)注多個(gè)地方 外呼系統(tǒng)電銷專用 優(yōu)質(zhì)地圖標(biāo)注 千呼電銷機(jī)器人價(jià)格

公用表表達(dá)式簡介:

公用表表達(dá)式 (CTE) 可以認(rèn)為是在單個(gè) SELECT、INSERT、UPDATE、DELETE 或 CREATE VIEW 語句的執(zhí)行范圍內(nèi)定義的臨時(shí)結(jié)果集。CTE 與派生表類似,具體表現(xiàn)在不存儲(chǔ)為對(duì)象,并且只在查詢期間有效。與派生表的不同之處在于,公用表表達(dá)式 (CTE) 具有一個(gè)重要的優(yōu)點(diǎn),那就是能夠引用其自身,從而創(chuàng)建遞歸 CTE。遞歸 CTE 是一個(gè)重復(fù)執(zhí)行初始 CTE 以返回?cái)?shù)據(jù)子集直到獲取完整結(jié)果集的公用表表達(dá)式。

下面先創(chuàng)建一個(gè)表,并插入一些數(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','超級(jí)管理員','0' union 
select '2','管理員A','1' union 
select '3','管理員B','2' union 
select '4','會(huì)員AA','2' union 
select '5','會(huì)員AB','2' union 
select '6','會(huì)員BA','3' union 
select '7','會(huì)員BB','3' union 
select '8','用戶AAA','4' union 
select '9','用戶BBA','7' 
-- 創(chuàng)建一個(gè)復(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é)點(diǎn)的所有子孫節(jié)點(diǎn):

使用普通 sql 語句實(shí)現(xiàn):

declare @level int
declare @node int
declare @ResTab table
(
 node int not null,
 lv  int not null 
)
set @level=0  -- 表示初始的等級(jí)
set @node=3   --表示初始的節(jié)點(diǎn)ID,即從指定的哪個(gè)節(jié)點(diǎn)開始查找
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é)點(diǎn)ID(3),查找父節(jié)點(diǎn)ID(即字段 ParentId)等于指定的節(jié)點(diǎn)ID,如果有就插入,并繼續(xù)循環(huán)。

PS:lv=@level-1 是重點(diǎn),不然會(huì)進(jìn)入死循環(huán),作用就是限制只插入一次。

如果需要限制循環(huán)的次數(shù),即遞歸的層數(shù),那么只需要在 while 條件里面添加一個(gè)限制即可。如下:

declare @level int
declare @node int
declare @num int
declare @ResTab table
(
 node int not null,
 lv  int not null 
)
set @level=0  -- 表示初始的等級(jí)
set @node=3   --表示初始的節(jié)點(diǎn)ID,即從指定的哪個(gè)節(jié)點(diǎn)開始查找
set @num=1  -- 指定遞歸層級(jí),即循環(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

當(dāng)然,如果指定了循環(huán)次數(shù),就可以不用 while 判斷語句的 @@rowcount>0 了。

使用 SQL CTE 實(shí)現(xiàn):

declare @node int 
set @node=3;
with temp_cte
as
(
 select Id,Name,0 lv  -- 查詢出“根節(jié)點(diǎn)”,即指定的起始節(jié)點(diǎn)
 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é)點(diǎn)”,即指定的起始節(jié)點(diǎn)
 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é)點(diǎn)的所有祖先節(jié)點(diǎn):

使用普通 sql 語句實(shí)現(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 -- 表示初始的等級(jí)
set @node=8   --表示初始的節(jié)點(diǎn)ID,即從指定的哪個(gè)節(jié)點(diǎn)開始查找
set @num=2  -- 指定遞歸層級(jí),即循環(huán)的次數(shù)
while(@level=@num and @node is not null) -- 如果為空就表示沒有查到父級(jí)了
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 實(shí)現(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é)點(diǎn)”,即指定的起始節(jié)點(diǎn)
 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 公用表表達(dá)式(CTE)實(shí)現(xiàn)遞歸的方法,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

您可能感興趣的文章:
  • 使用SqlServer CTE遞歸查詢處理樹、圖和層次結(jié)構(gòu)
  • 使用SQLSERVER 2005/2008 遞歸CTE查詢樹型結(jié)構(gòu)的方法

標(biāo)簽:威海 七臺(tái)河 益陽 來賓 銅仁 天水 宿州 防疫戰(zhàn)設(shè)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SQL Server 公用表表達(dá)式(CTE)實(shí)現(xiàn)遞歸的方法》,本文關(guān)鍵詞  SQL,Server,公用,表,表達(dá)式,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SQL Server 公用表表達(dá)式(CTE)實(shí)現(xiàn)遞歸的方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于SQL Server 公用表表達(dá)式(CTE)實(shí)現(xiàn)遞歸的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    辽阳市| 九江市| 浠水县| 辽阳市| 茶陵县| 肇东市| 溧阳市| 错那县| 江源县| 肃宁县| 宝山区| 绍兴县| 锦州市| 红原县| 侯马市| 佛学| 蚌埠市| 庆安县| 白水县| 利辛县| 卫辉市| 开化县| 綦江县| 通许县| 许昌县| 九江县| 通辽市| 博白县| 本溪市| 瓮安县| 滦南县| 东乡族自治县| 闻喜县| 吴江市| 阿拉善盟| 祁门县| 抚顺市| 泰州市| 阳东县| 丰原市| 古交市|