亚洲AV无码乱码在线观看不卡|最新大伊香蕉精品视频在线|亚洲不卡av一区二区|国产美女无遮挡免费

登錄論壇 | 注冊(cè)會(huì)員 設(shè)為首頁 | 收藏本站
當(dāng)前位置 : 首頁>軟件學(xué)院>數(shù)據(jù)庫>SQL>正文
 
三種實(shí)現(xiàn)方法實(shí)現(xiàn)數(shù)據(jù)表中遍歷尋找子節(jié)點(diǎn)

http://www.yibo1263.com 2008/5/18 8:39:33  來源:ccident  編輯:葉子
 

數(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

收藏】【打印】【進(jìn)入論壇
  相關(guān)文章:

 
 
 
最新文章

搶先蘋果,消息稱英特爾芯片采用臺(tái)積電
三星揭曉業(yè)內(nèi)首款單條 512GB DDR5 內(nèi)存
vivo 高端新機(jī)爆料:120Hz 曲面屏 + 天
vivo Y21 在印度正式上市:Helio P35 芯
微星推出 GeForce RTX 3080 Sea Hawk X
消息稱三星 Galaxy Tab S8 系列平板將放
機(jī)械革命推出 F6 輕薄本:16 英寸全面屏
英特爾 12 代 Alder Lake CPU 600 系列
雷軍:向小米手機(jī) 1 首批用戶每人贈(zèng)送價(jià)
小米李明談?dòng)脩舯惶叱?nbsp;MIUI 測(cè)試版:大

推薦文章
1
2
3
4
5
6
7
8
9
10
叛逆嫩模性感寫真
宮如敏不雅照瘋傳 看張馨予韓一菲獸獸誰
不懼孔子搶位 阿凡達(dá)游戲影音配置推薦
2015第十七屆“東北安博會(huì)”火爆招商
第十六屆東北國際公共安全防范產(chǎn)品博覽
2016年第五屆中國國際商業(yè)信息化博覽會(huì)
2016年第五屆中國國際POS機(jī)及相關(guān)設(shè)備展
互聯(lián)網(wǎng)電視熟了嗎 2013最火電視深解析
桑達(dá)獲邀出席2015中國(廣州)國際POS機(jī)
寶獲利報(bào)名參加“2015年度中國POS機(jī)行業(yè)
八卦圖解 More>>
叛逆嫩模性感寫真 宮如敏不雅照瘋傳 看張馨予韓一菲
周偉童魔鬼身材日本性感寫真圖  聯(lián)想V360筆記本模特寫真
清原| 佛坪县| 台前县| 滨州市| 晋中市| 沁源县| 横峰县| 兴安县| 闻喜县| 长武县| 舟山市| 陇川县| 四会市| 南京市| 翼城县| 扶沟县| 始兴县| 苍梧县| 青龙| 潢川县| 万山特区| 黎川县| 临朐县| 调兵山市| 灵石县| 重庆市| 阳东县| 军事| 文昌市| 墨玉县| 武汉市| 永城市| 达州市| 酉阳| 阜城县| 如皋市| 霍山县| 南阳市| 平昌县| 朔州市| 阿拉善左旗|