三種實(shí)現(xiàn)方法實(shí)現(xiàn)數(shù)據(jù)表中遍歷尋找子節(jié)點(diǎn)
更新時(shí)間:2008年05月17日 21:48:44 作者:
數(shù)據(jù)表中遍歷尋找子節(jié)點(diǎn)的三種實(shí)現(xiàn)方法:
示例問題如下:
表結(jié)構(gòu):
Id ParentId
1 0
2 1
3 2
......
針對(duì)該表結(jié)構(gòu)解釋如下:
1的父節(jié)點(diǎn)為0,
2的父節(jié)點(diǎn)為1,
3的父節(jié)點(diǎn)為2
......
以此類推,要求給定一個(gè)父節(jié)點(diǎn)的值,比如1,
用SQL語句查詢的到該父結(jié)點(diǎn)下的所有子節(jié)點(diǎn)
下面的Sql是在Sql Server下調(diào)試通過的,如果是Oracle,則有Connect By可以實(shí)現(xiàn).
建立測(cè)試表:
Drop Table DbTree
Create Table DbTree
(
[Id] Int,
[Name] NVarChar(20),
[ParentId] Int
)
插入測(cè)試數(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)
實(shí)現(xiàn)方法一:
代碼如下:
Declare @Id Int
Set @Id = 1 ---在次修改父節(jié)點(diǎn)
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
實(shí)現(xiàn)方法二:
代碼如下:
Create Table #AllRow
(
Id Int,
ParentId Int
)
Declare @Id Int
Set @Id = 1 ---在次修改父節(jié)點(diǎn)
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
實(shí)現(xiàn)方法三:
代碼如下:
在Sql Server2005中其實(shí)提供了CTE[公共表表達(dá)式]來實(shí)現(xiàn)遞歸:
關(guān)于CTE的使用請(qǐng)查MSDN
Declare @Id Int
Set @Id = 3; ---在次修改父節(jié)點(diǎn)
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
......
針對(duì)該表結(jié)構(gòu)解釋如下:
1的父節(jié)點(diǎn)為0,
2的父節(jié)點(diǎn)為1,
3的父節(jié)點(diǎn)為2
......
以此類推,要求給定一個(gè)父節(jié)點(diǎn)的值,比如1,
用SQL語句查詢的到該父結(jié)點(diǎn)下的所有子節(jié)點(diǎn)
下面的Sql是在Sql Server下調(diào)試通過的,如果是Oracle,則有Connect By可以實(shí)現(xiàn).
建立測(cè)試表:
Drop Table DbTree
Create Table DbTree
(
[Id] Int,
[Name] NVarChar(20),
[ParentId] Int
)
插入測(cè)試數(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)
實(shí)現(xiàn)方法一:
代碼如下:
Declare @Id Int
Set @Id = 1 ---在次修改父節(jié)點(diǎn)
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
實(shí)現(xiàn)方法二:
代碼如下:
Create Table #AllRow
(
Id Int,
ParentId Int
)
Declare @Id Int
Set @Id = 1 ---在次修改父節(jié)點(diǎn)
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
實(shí)現(xiàn)方法三:
代碼如下:
在Sql Server2005中其實(shí)提供了CTE[公共表表達(dá)式]來實(shí)現(xiàn)遞歸:
關(guān)于CTE的使用請(qǐng)查MSDN
Declare @Id Int
Set @Id = 3; ---在次修改父節(jié)點(diǎn)
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)了三個(gè)臨時(shí)表,為了安全這里為大家分享下sql server 2000中禁止創(chuàng)建表的方法,網(wǎng)上都么有的,腳本之家小編原創(chuàng)2015-07-07快速實(shí)現(xiàn)SQL Server數(shù)據(jù)庫恢復(fù)備份
這篇文章主要為大家詳細(xì)介紹了如何快速實(shí)現(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)資料,文中通過實(shí)例介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03SQL server高并發(fā)生成唯一訂單號(hào)的方法實(shí)現(xiàn)
這篇文章主要介紹了SQL server高并發(fā)生成唯一訂單號(hào)的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02sqlserver主鍵設(shè)計(jì)的注意點(diǎn)
在數(shù)據(jù)庫設(shè)計(jì)中,主鍵用于惟一地標(biāo)識(shí)表中的某一條記錄2012-07-07詳解SQL Server 中 JSON_MODIFY 的使用
SQL Server 從 2016 開始支持了一些 JSON操作,最近的項(xiàng)目里也是好多地方字段直接存成了 JSON,需要了解一下怎么在SQL Server 中操作 JSON.這篇文章主要介紹了SQL Server 中 JSON_MODIFY 的使用,需要的朋友可以參考下2019-11-11Windows下使用性能監(jiān)視器監(jiān)控SqlServer的常見指標(biāo)
這篇文章主要介紹了Windows下使用性能監(jiān)視器監(jiān)控SqlServer的常見指標(biāo),常見指標(biāo)包括Buffer Cache Hit Ratio、Pages/sec、 Available Bytes、Disk Time、Avg. Disk Queue Length、Processor Time、Processor Queue Length等,需要的朋友可以參考下2015-02-02