在實際運用中經常會創建這樣的結構表Category(Id, ParentId, Name),
特別是用於樹形結構時(菜單樹,權限樹..),
這種表設計自然而然地會用到遞歸,
若是在程序中進行遞歸(雖然在程序中遞歸真的更方便一些),
無論是通過ADO.NET簡單sql查找還是ORM屬性關聯都會執行多次sql語句,
難免會造成一些性能上的損耗,所以干脆使用sql的函數來解決這個問題,用函數返回我們最終需要的結果。
數據准備
CREATE TABLE Region ( Id INT IDENTITY PRIMARY KEY, Name NVARCHAR(20), ParentId INT ); GO insert into Region(Name,ParentId) values('廣東',NULL) insert into Region(Name,ParentId) values('深圳',1) insert into Region(Name,ParentId) values('惠州',2) insert into Region(Name,ParentId) values('羅湖區',2) insert into Region(Name,ParentId) values('福田區',2) insert into Region(Name,ParentId) values('龍崗區',2) insert into Region(Name,ParentId) values('惠陽區',3) insert into Region(Name,ParentId) values('龍門縣',3) insert into Region(Name,ParentId) values('華強北',5) insert into Region(Name,ParentId) values('體育館',5) SELECT * FROM dbo.Region AS R
1.查詢父節點的所有子節點
/* * summary:遞歸獲取所有子節點 */ CREATE function GetRecursiveChildren ( @Id int ) returns @t table(Id int,ParentId int,Name nvarchar(20), [Level] int) begin declare @i int set @i = 1 --根節點,Level = 0 insert into @t select @Id,@id,(select Name from Region where Id = @id),0 --直屬子節點,Level = 1 insert into @t select Id,ParentId,Name,@i from Region where ParentId = @Id --如果沒有新的值插入,循環結束 while @@rowcount<>0 begin set @i = @i + 1; insert into @t select a.Id,a.ParentId,a.Name,@i from Region a, @t b where a.ParentId = b.Id and b.Level = @i - 1 end return end go --調用函數 select * from GetRecursiveChildren(3)
--CTE(公用表表達式)實現 declare @id int set @id = 3 ;with t as--如果CTE前面有語句,需要用分號隔斷 ( select Id, ParentId, Name from Region where Id = @id union all select r1.Id,r1.ParentId,r1.Name from Region r1 join t as r2 on r1.ParentId = r2.Id ) select * from t order by Id
2.根據子節點追溯根節點
create function GetRecursiveParent ( @Id int ) returns @t table(Id int,ParentId int,Name nvarchar(20), [Level] int) as begin declare @i int set @i = 1 --插入末節點,Level = 0 insert into @t select @Id,@id,(select Name from Region where Id = @id),0 --插入末節點的父節點,Level = 1 insert into @t select Id,ParentId,Name,@i from Region where Id = (select ParentId from Region where Id = @Id) --如果沒有新的值插入,循環結束 while @@rowcount<>0 begin set @i = @i + 1; insert into @t select a.Id,a.ParentId,a.Name,@i from Region a, @t b where a.Id = b.ParentId and b.Level = @i - 1 end return end go --調用函數 select * from GetRecursiveParent(8) go
3.根據導航數據查詢節點
create function GetLevel ( @Id int ) returns @level table(IdLevel varchar(100),NameLevel nvarchar(200)) as begin declare @IdLevel varchar(100),@NameLevel nvarchar(200),@Name nvarchar(50) select @IdLevel = cast(@Id as varchar(10)) select @NameLevel = (select Name from Region where Id = @Id) while(exists(select Id,ParentId from Region where Id = (select ParentId from Region where Id = @Id))) begin select @Id = Id,@Name = Name from Region where Id = (select ParentId from Region where Id = @Id) select @IdLevel = cast(@Id as varchar(10)) + '>' + @IdLevel select @NameLevel = @Name + '>' + @NameLevel end insert into @level select @IdLevel,@NameLevel return end go --調用函數 select * from GetLevel(10) go