我已解决这问题了,谢谢你的提示,查询方法为:
select distinct a.id from tb a left join tb b on a.pid=b.id where a.pid=@***
union
select distinct a.id from tb a left join tb b on a.pid=b.id where b.pid=@***
[create] table t(id int,name char(1),parent_id int,sfmx int)
insert into t select 1,'a',0,0
insert into t select 2,'b',1,0
insert into t select 3,'c',1,0
insert into t select 4,'d',1,1
insert into t select 5,'e',2,1
insert into t select 6,'f',3,1
go
select a.id,isnull(c.name+'-','')+isnull(b.name+'-','')+isnull(a.name,'')
from t a
left join t b on a.parent_id=b.id
left join t c on b.parent_id=c.id