34,837
社区成员




declare @id uniqueidentifier;
set @id = '00000000-0000-0000-0000-000000000000';
;with t as (
select id,pid, name, 0 level from tb where id=@id
union all
select tb.id, tb.pid, tb.name, t.level+1 from t, tb where tb.pid=t.id
)
select id, pid, name from t order by level;
declare @id uniqueidentifier;
set @id = ...
;with t as (
select id,pid, name, 0 level from tb where id=@id
union all
select tb.id, tb.pid, tb.name, t.level+1 from t, tb where tb.pid=t.id
)
select id, pid, name from t order by level;