三種實現(xiàn)方法實現(xiàn)數(shù)據(jù)表中遍歷尋找子節(jié)點
更新時間:2008年05月17日 21:48:44 作者:
數(shù)據(jù)表中遍歷尋找子節(jié)點的三種實現(xiàn)方法:
示例問題如下:
表結(jié)構(gòu):
Id ParentId
1 0
2 1
3 2
......
針對該表結(jié)構(gòu)解釋如下:
1的父節(jié)點為0,
2的父節(jié)點為1,
3的父節(jié)點為2
......
以此類推,要求給定一個父節(jié)點的值,比如1,
用SQL語句查詢的到該父結(jié)點下的所有子節(jié)點
下面的Sql是在Sql Server下調(diào)試通過的,如果是Oracle,則有Connect By可以實現(xiàn).
建立測試表:
Drop Table DbTree
Create Table DbTree
(
[Id] Int,
[Name] NVarChar(20),
[ParentId] Int
)
插入測試數(shù)據(jù):
Insert Into DbTree ([Id],[ParentId]) Values (1,0)
Insert Into DbTree ([Id],[ParentId]) Values (2,1)
Insert Into DbTree ([Id],[ParentId]) Values (3,1)
Insert Into DbTree ([Id],[ParentId]) Values (4,3)
Insert Into DbTree ([Id],[ParentId]) Values (5,4)
Insert Into DbTree ([Id],[ParentId]) Values (6,7)
Insert Into DbTree ([Id],[ParentId]) Values (8,5)
實現(xiàn)方法一:
代碼如下:
Declare @Id Int
Set @Id = 1 ---在次修改父節(jié)點
Select * Into #Temp From DbTree Where ParentId In (@Id)
Select * Into #AllRow From DbTree Where ParentId In (@Id) --1,2
While Exists(Select * From #Temp)
Begin
Select * Into #Temp2 From #Temp
Truncate Table #Temp
Insert Into #Temp Select * From DbTree Where ParentId In (Select Id From #Temp2)
Insert Into #AllRow Select * From #Temp
Drop Table #Temp2
End
Select * From #AllRow Order By Id
Drop Table #Temp
Drop Table #AllRow
實現(xiàn)方法二:
代碼如下:
Create Table #AllRow
(
Id Int,
ParentId Int
)
Declare @Id Int
Set @Id = 1 ---在次修改父節(jié)點
Delete #AllRow
--頂層自身
Insert Into #AllRow (Id,ParentId) Select @Id, @Id
While @@RowCount > 0
Begin
Insert Into #AllRow (Id,ParentId)
Select B.Id,A.Id
From #AllRow A,DbTree B
Where A.Id = B.ParentId And
Not Exists (Select Id From #AllRow Where Id = B.Id And ParentId = A.Id)
End
Delete From #AllRow Where Id = @Id
Select * From #AllRow Order By Id
Drop Table #AllRow
實現(xiàn)方法三:
代碼如下:
在Sql Server2005中其實提供了CTE[公共表表達(dá)式]來實現(xiàn)遞歸:
關(guān)于CTE的使用請查MSDN
Declare @Id Int
Set @Id = 3; ---在次修改父節(jié)點
With RootNodeCTE(Id,ParentId)
As
(
Select Id,ParentId From DbTree Where ParentId In (@Id)
Union All
Select DbTree.Id,DbTree.ParentId From RootNodeCTE
Inner Join DbTree
On RootNodeCTE.Id = DbTree.ParentId
)
Select * From RootNodeCTE
表結(jié)構(gòu):
Id ParentId
1 0
2 1
3 2
......
針對該表結(jié)構(gòu)解釋如下:
1的父節(jié)點為0,
2的父節(jié)點為1,
3的父節(jié)點為2
......
以此類推,要求給定一個父節(jié)點的值,比如1,
用SQL語句查詢的到該父結(jié)點下的所有子節(jié)點
下面的Sql是在Sql Server下調(diào)試通過的,如果是Oracle,則有Connect By可以實現(xiàn).
建立測試表:
Drop Table DbTree
Create Table DbTree
(
[Id] Int,
[Name] NVarChar(20),
[ParentId] Int
)
插入測試數(shù)據(jù):
Insert Into DbTree ([Id],[ParentId]) Values (1,0)
Insert Into DbTree ([Id],[ParentId]) Values (2,1)
Insert Into DbTree ([Id],[ParentId]) Values (3,1)
Insert Into DbTree ([Id],[ParentId]) Values (4,3)
Insert Into DbTree ([Id],[ParentId]) Values (5,4)
Insert Into DbTree ([Id],[ParentId]) Values (6,7)
Insert Into DbTree ([Id],[ParentId]) Values (8,5)
實現(xiàn)方法一:
代碼如下:
Declare @Id Int
Set @Id = 1 ---在次修改父節(jié)點
Select * Into #Temp From DbTree Where ParentId In (@Id)
Select * Into #AllRow From DbTree Where ParentId In (@Id) --1,2
While Exists(Select * From #Temp)
Begin
Select * Into #Temp2 From #Temp
Truncate Table #Temp
Insert Into #Temp Select * From DbTree Where ParentId In (Select Id From #Temp2)
Insert Into #AllRow Select * From #Temp
Drop Table #Temp2
End
Select * From #AllRow Order By Id
Drop Table #Temp
Drop Table #AllRow
實現(xiàn)方法二:
代碼如下:
Create Table #AllRow
(
Id Int,
ParentId Int
)
Declare @Id Int
Set @Id = 1 ---在次修改父節(jié)點
Delete #AllRow
--頂層自身
Insert Into #AllRow (Id,ParentId) Select @Id, @Id
While @@RowCount > 0
Begin
Insert Into #AllRow (Id,ParentId)
Select B.Id,A.Id
From #AllRow A,DbTree B
Where A.Id = B.ParentId And
Not Exists (Select Id From #AllRow Where Id = B.Id And ParentId = A.Id)
End
Delete From #AllRow Where Id = @Id
Select * From #AllRow Order By Id
Drop Table #AllRow
實現(xiàn)方法三:
代碼如下:
在Sql Server2005中其實提供了CTE[公共表表達(dá)式]來實現(xiàn)遞歸:
關(guān)于CTE的使用請查MSDN
Declare @Id Int
Set @Id = 3; ---在次修改父節(jié)點
With RootNodeCTE(Id,ParentId)
As
(
Select Id,ParentId From DbTree Where ParentId In (@Id)
Union All
Select DbTree.Id,DbTree.ParentId From RootNodeCTE
Inner Join DbTree
On RootNodeCTE.Id = DbTree.ParentId
)
Select * From RootNodeCTE
相關(guān)文章
大數(shù)據(jù)量分頁存儲過程效率測試附測試代碼與結(jié)果
在項目中,我們經(jīng)常遇到或用到分頁,那么在大數(shù)據(jù)量(百萬級以上)下,哪種分頁算法效率最優(yōu)呢?我們不妨用事實說話。2010-07-07
SQL Server 2012 FileTable 新特性詳解
FileTable是基于FILESTREAM的一個特性。本文給大家介紹SQL Server 2012 FileTable 新特性詳解,非常不錯,感興趣的朋友一起學(xué)習(xí)吧2016-08-08
SQL?Server數(shù)據(jù)表模糊查詢(like用法)以及查詢函數(shù)詳解
使用SQL Server查詢時經(jīng)常會使用模糊查詢,需要查詢包含的指定字符串內(nèi)容,這篇文章主要給大家介紹了關(guān)于SQL?Server數(shù)據(jù)表模糊查詢(like用法)以及查詢函數(shù)的相關(guān)資料,需要的朋友可以參考下2024-05-05
sql server數(shù)據(jù)庫中raiserror函數(shù)用法的詳細(xì)介紹
這篇文章主要介紹了sql server數(shù)據(jù)庫中raiserror函數(shù)用法的詳細(xì)介紹,raiserror用于拋出一個異?;蝈e誤,讓這個錯誤可以被程序捕捉到。對此感興趣的可以了解一下2020-07-07
SQL?Server中元數(shù)據(jù)函數(shù)的用法
這篇文章介紹了SQL?Server中元數(shù)據(jù)函數(shù)的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
Sql Server 開窗函數(shù)Over()的使用實例詳解
這篇文章主要介紹了Sql Server 開窗函數(shù)Over()的使用,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-05-05
SQL Server實現(xiàn)跨庫跨服務(wù)器訪問的方法
這篇文章主要給大家介紹了關(guān)于SQL Server實現(xiàn)跨庫跨服務(wù)器訪問的方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用SQL Server具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06

