22,209
社区成员
发帖
与我相关
我的任务
分享
create table test(ID int, Name varchar(10), ParentID int)
go
insert into test values(1, '张三', 0),(2, '李四', 1),(3, '王五', 2)
go
with m as (
select t.ID, cast(t.name as nvarchar(30)) name, t.ParentID
from test t where t.ParentID = 0
union all
select t.ID, cast(m.Name + '/' + t.Name as nvarchar(30)), t.ParentID
from test t, m where t.ParentID = m.ID
)
select * from m
go
drop table test
go
(3 行受影响)
ID name ParentID
----------- ------------------------------ -----------
1 张三 0
2 张三/李四 1
3 张三/李四/王五 2
(3 行受影响)