日期:2014-05-18 浏览次数:20464 次
if object_id('[tb]') is not null drop table [tb] create table [tb] (id int,name varchar(1),pid int) insert into [tb] select 1,'A',0 union all select 2,'B',1 union all select 3,'D',1 union all select 4,'C',2 union all select 5,'D',2 union all select 6,'A',4 union all select 7,'E',5 union all select 8,'F',5 GO ;with cte as ( select *,[path]=cast([name]+'->' as varchar(100)) ,[level] = 1 from tb where pid = 0 union all select a.*, cast(c.[path]+a.[name]+'->' as varchar(100)),[level]+1 from cte c ,tb a where a.pid = c.id ) select * from cte