三種實現(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[公共表表達式]來實現(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[公共表表達式]來實現(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)文章
sql server 2000中禁止創(chuàng)建表(權(quán)限設(shè)置方法)
最近發(fā)現(xiàn)數(shù)據(jù)庫中發(fā)現(xiàn)了三個臨時表,為了安全這里為大家分享下sql server 2000中禁止創(chuàng)建表的方法,網(wǎng)上都么有的,腳本之家小編原創(chuàng)2015-07-07快速實現(xiàn)SQL Server數(shù)據(jù)庫恢復(fù)備份
這篇文章主要為大家詳細介紹了如何快速實現(xiàn)SQL Server數(shù)據(jù)庫恢復(fù)備份的兩種方法,感興趣的小伙伴們可以參考一下2016-05-05SQL SERVER 2012新增函數(shù)之字符串函數(shù)FORMAT詳解
這篇文章主要給大家介紹了關(guān)于SQL SERVER 2012新增函數(shù)之字符串函數(shù)FORMAT的相關(guān)資料,文中通過實例介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03SQL server高并發(fā)生成唯一訂單號的方法實現(xiàn)
這篇文章主要介紹了SQL server高并發(fā)生成唯一訂單號的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02詳解SQL Server 中 JSON_MODIFY 的使用
SQL Server 從 2016 開始支持了一些 JSON操作,最近的項目里也是好多地方字段直接存成了 JSON,需要了解一下怎么在SQL Server 中操作 JSON.這篇文章主要介紹了SQL Server 中 JSON_MODIFY 的使用,需要的朋友可以參考下2019-11-11Windows下使用性能監(jiān)視器監(jiān)控SqlServer的常見指標
這篇文章主要介紹了Windows下使用性能監(jiān)視器監(jiān)控SqlServer的常見指標,常見指標包括Buffer Cache Hit Ratio、Pages/sec、 Available Bytes、Disk Time、Avg. Disk Queue Length、Processor Time、Processor Queue Length等,需要的朋友可以參考下2015-02-02