人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

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

發(fā)布日期:2021-12-14 05:20 | 文章來源:源碼中國

公用表表達式簡介:

公用表表達式 (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)站的支持!

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部