新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SQL遍歷父子關(guān)系表的測試
SQL遍歷父子關(guān)系表的方法未必人人都知道,下面就為您介紹一個SQL遍歷父子關(guān)系表的測試,希望可以讓您對SQL遍歷父子關(guān)系表有更深的認(rèn)識。

專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)埇橋區(qū)免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
--建立測試環(huán)境
- Create Table A
- (ID Int,
- fatherID Int,
- Name Varchar(10)
- )
- Insert A Select 1, NULL, 'tt'
- Union All Select 2, 1, 'aa'
- Union All Select 3, 1, 'bb'
- Union All Select 4, 2, 'cc'
- Union All Select 5, 2, 'gg'
- Union All Select 6, 4, 'yy'
- Union All Select 7, 4, 'jj'
- Union All Select 8, 7, 'll'
- Union All Select 9, NULL, 'uu'
- Union All Select 10, 9, 'oo'
- GO
--建立函數(shù)
- Create Function GetChildren(@ID Int)
- Returns @Tree Table (ID Int, fatherID Int, Name Varchar(10))
- As
- Begin
- Insert @Tree Select ID, fatherID, Name From A Where fatherID = @ID
- While @@Rowcount > 0
- Insert @Tree Select A.ID, A.fatherID, A.Name From A A Inner Join @Tree B On A.fatherID = B.ID And A.ID Not In (Select ID From @Tree)
- Return
- End
- GO
--測試
- Select * From dbo.GetChildren(1)
- GO
--刪除測試環(huán)境
- Drop Table A
- Drop Function GetChildren
--結(jié)果
- /*
- IDfatherIDName
- 21aa
- 31bb
- 42cc
- 52gg
- 64yy
- 74jj
- 87ll
- */
【編輯推薦】
SQL Server視圖的使用
SQL SERVER內(nèi)部函數(shù)大全
SQL Server變量賦值的方法
詳解SQL Server全局變量
動態(tài)sql中使用臨時表的實(shí)例
網(wǎng)站題目:SQL遍歷父子關(guān)系表的測試
URL地址:http://www.dlmjj.cn/article/cdisodh.html


咨詢
建站咨詢
