查詢指定節點及其所有子節點的函式

2021-04-18 08:17:45 字數 3481 閱讀 6927

--測試資料

create table tb(id char(3),pid char(3),name nvarchar(10))

insert tb select '001',null ,'山東省'

union all select '002','001','煙台市'

union all select '004','002','招遠市'

union all select '003','001','青島市'

union all select '005',null ,'四會市'

union all select '006','005','清遠市'

union all select '007','006','小分市'

go--查詢指定節點及其所有子節點的函式

create function f_cid(@id char(3))

returns @t_level table(id char(3),level int)

asbegin

declare @level int

set @level=1

insert @t_level select @id,@level

while @@rowcount>0

begin

set @level=@level+1

insert @t_level select a.id,@level

from tb a,@t_level b

where a.pid=b.id

and b.level=@level-1

endreturn

endgo

--呼叫函式查詢002及其所有子節點

select a.*

from tb a,f_cid('002') b

where a.id=b.id

/*--結果

id pid name

------ ------- ----------

002 001 煙台市

004 002 招遠市

--*/

8.2.4 查詢指定節點的所有父節點的示例函式.sql

create function f_pid(@id char(3))

returns @t_level table(id char(3),level int)

asbegin

declare @level int

set @level=1

insert @t_level select @id,@level

while @@rowcount>0

begin

set @level=@level+1

insert @t_level select a.pid,@level

from tb a,@t_level b

where a.id=b.id

and b.level=@level-1

endreturn

endgo

--上面的使用者定義函式可以處理乙個節點有多個父節點的情況,對於標準的樹形資料而言,由於每個節點僅有乙個父節點,所以也可以通過下面的使用者定義函式實現查詢標準樹形資料的父節點。

create function f_pid(@id char(3))

returns @t_level table(id char(3))

asbegin

insert @t_level select @id

select @id=pid from tb

where id=@id

and pid is not null

while @@rowcount>0

begin

insert @t_level select @id

select @id=pid from tb

where id=@id

and pid is not null

endreturn

end

樹形資料深度排序處理示例(遞迴法)

--測試資料

create table tb(id char(3),pid char(3),name nvarchar(10))

insert tb select '001',null ,'山東省'

union all select '002','001','煙台市'

union all select '004','002','招遠市'

union all select '003','001','青島市'

union all select '005',null ,'四會市'

union all select '006','005','清遠市'

union all select '007','006','小分市'

go--廣度搜尋排序函式

create function f_sort(@id char(3)=null,@sort int=1)

returns @t_level table(id char(3),sort int)

asbegin

declare tb cursor local

forselect id from tb

where pid=@id

or(@id is null and pid is null)

open tb

fetch tb into @id

while @@fetch_status=0

begin

insert @t_level values(@id,@sort)

set @sort=@sort+1

if @@nestlevel<32 --如果遞迴層數未超過32層(遞迴最大允許32層)

begin

--遞迴查詢當前節點的子節點

insert @t_level select * from f_sort(@id,@sort)

set @sort=@sort+@@rowcount --排序號加上子節點個數

endfetch tb into @id

endreturn

endgo

--顯示結果

select a.*

from tb a,f_sort(default,default) b

where a.id=b.id

order by b.sort

/*--結果

id pid name

------ --------- ----------

001 null 山東省

002 001 煙台市

004 002 招遠市

003 001 青島市

005 null 四會市

006 005 清遠市

007 006 小分市

--*/

MySQL中根據父節點查詢其所有子節點

drop function if exists fun get children delimiter create definer root 127.0.0.1 function fun get children i id int 32 returns varchar 20000 charset u...

查詢指定節點的所有子節點的示例函式 sql

測試資料 create table tb id char 3 pid char 3 name nvarchar 10 insert tb select 001 null 山東省 union all select 002 001 煙台市 union all select 004 002 招遠市 uni...

查詢指定節點的所有子節點的示例函式 sql

測試資料 create table tb id char 3 pid char 3 name nvarchar 10 insert tb select 001 null 山東省 union all select 002 001 煙台市 union all select 004 002 招遠市 uni...