1
select
*
from system_part order by id
2 exec selpart
3 create proc selpart
4 as
5 begin
6 declare @id int
7 declare @level int
8 declare @t table(id int ,[level] int )
9 set @id = 382
10 set @level = 1
11 insert into @t values (@id,@level)
12 while @@rowcount > 0
13 begin
14 set @level = @level + 1
15 insert into @t
16 select a.id,@level
17 from system_part as a
18 join @t as b on a.parentid = b.id where b.[level] = @level - 1
19
20 end
21 select * from system_part where id in (select id from @t)
22 end
2 exec selpart
3 create proc selpart
4 as
5 begin
6 declare @id int
7 declare @level int
8 declare @t table(id int ,[level] int )
9 set @id = 382
10 set @level = 1
11 insert into @t values (@id,@level)
12 while @@rowcount > 0
13 begin
14 set @level = @level + 1
15 insert into @t
16 select a.id,@level
17 from system_part as a
18 join @t as b on a.parentid = b.id where b.[level] = @level - 1
19
20 end
21 select * from system_part where id in (select id from @t)
22 end
如上~~~