22,209
社区成员
发帖
与我相关
我的任务
分享
drop table #tb;
Create table #tb(id int identity(1,1),ilden int,sname nvarchar(200),itopid int);
Insert Into #tb(ilden,sname,itopid)
Select null,'基础知识',2 Union All
Select 2,'SqlServer',3 Union All
Select 3,'Book',null
with t as
(
Select sname,ilden,itopid From #tb Where itopid is null
Union All
Select Cast(t.sname+ N'/'+#tb.sname as Nvarchar(200))as sname,#tb.ilden,#tb.itopid
From t
Inner Join #tb
On t.ilden = #tb.itopid)
Select *
From t
--------------------------------------------------------
--Book 3 NULL
--Book/SqlServer 2 3
--Book/SqlServer/基础知识 NULL 2
--
--------------------------------------
是不是这样?