| 发表于:2007-07-17 09:48:184楼 得分:0 |
create table tb ( id int identity(5,1), sb_1 int, sb_2 int ) insert into tb select 23,24 union all select 23,25 union all select 24,32 union all select 32,26 union all select 26,33 go --查找所有父结点及子节点的问题,当前假设sb_1为父结点,sb_2为子结点建立函数,查找所有子结点 create function f_pid(@sb_1 int) returns @t_level table(sb_1 int,level int) as begin declare @level int set @level=1 insert @t_level select @sb_1,@level while @@rowcount> 0 begin set @level=@level+1 insert @t_level select a.sb_2,@level from tb a,@t_level b where a.sb_1=b.sb_1 and b.level=@level-1 end return end go select * from dbo.f_pid(23) sb_1 level ----------- ----------- 23 1 24 2 25 2 32 3 26 4 33 5 (6 行受影响) 如要查子结点,在函数中做相应操作即可 | | |
|