溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SQL Server中怎么利用公用表表達式實現(xiàn)遞歸

發(fā)布時間:2021-08-04 15:26:17 來源:億速云 閱讀:130 作者:Leah 欄目:數(shù)據(jù)庫

這篇文章給大家介紹SQL Server中怎么利用公用表表達式實現(xiàn)遞歸,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

公用表表達式簡介:

公用表表達式 (CTE) 可以認為是在單個 SELECT、INSERT、UPDATE、DELETE 或 CREATE VIEW 語句的執(zhí)行范圍內(nèi)定義的臨時結(jié)果集。CTE 與派生表類似,具體表現(xiàn)在不存儲為對象,并且只在查詢期間有效。與派生表的不同之處在于,公用表表達式 (CTE) 具有一個重要的優(yōu)點,那就是能夠引用其自身,從而創(chuàng)建遞歸 CTE。遞歸 CTE 是一個重復執(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)建一個復合聚集索引create clustered index Clu_Role_CTE_Indexon 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 intdeclare @node intdeclare @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=@nodewhile(@@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)連接)和自連接endselect 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 intdeclare @node intdeclare @num intdeclare @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=@nodewhile(@@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)連接)和自連接endselect 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_cteas( 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 intset @node=3;set @num=1;with temp_cteas( 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 intdeclare @node intdeclare @num intdeclare @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=@nodeendselect 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 intset @node=8;set @num=2;with temp_cteas( 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中怎么利用公用表表達式實現(xiàn)遞歸就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI