28,391
社区成员
发帖
与我相关
我的任务
分享
create table tb(id int,fid int,name nvarchar(10))
insert into tb select 1,0,'百度知道'
insert into tb select 2,1,'电脑/网络'
insert into tb select 3,2,'程序设计'
insert into tb select 4,3,'数据库程序设计'
insert into tb select 5,1,'电脑/单机'
go
declare @current nvarchar(10)
set @current='程序设计'
;with cte as(
select * from tb where name=@current
union all
select a.* from tb a inner join cte b on a.id=b.fid
),c1 as(
select top 100 name from cte order by fid
)
select stuff((select '_'+[name] from c1 for xml path('')),1,1,'')
go
drop table tb
/*
--------------------------------------------------------------------------------------
百度知道_电脑/网络_程序设计
(1 行受影响)
*/
create table tb(id int,fid int,name nvarchar(10))
insert into tb select 1,0,'百度知道'
insert into tb select 2,1,'电脑/网络'
insert into tb select 3,2,'程序设计'
insert into tb select 4,3,'数据库程序设计'
insert into tb select 5,1,'电脑/单机'
go
declare @current nvarchar(10)
set @current='程序设计'
;with cte as(
select * from tb where name=@current
union all
select a.* from tb a inner join cte b on a.id=b.fid
),c1 as(
select top 100 name from cte order by fid desc
)
select stuff((select '_'+[name] from c1 for xml path('')),1,1,'')
go
drop table tb
/*
--------------------------------------------------------------------------------------
程序设计_电脑/网络_百度知道
(1 行受影响)
*/