日期:2014-05-17 浏览次数:20405 次
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#A') is null
drop table #A
Go
Create table #A([Class] nvarchar(11))
Insert #A
select N'0,1,2,3,4,7'
Go
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#B') is null
drop table #B
Go
Create table #B([序号] int,[名称] nvarchar(2))
Insert #B
select 0,N'苹果' union all
select 1,N'雪梨' union all
select 2,N'香蕉' union all
select 3,N'西瓜' union all
select 4,N'葡萄' union all
select 5,N'草莓' union all
select 6,N'芒果' union all
select 7,N'石榴'
Go
Select [Class],Name=stuff((select ','+[名称] from #B where ','+[Class]+',' like '%,'+rtrim([序号])+',%' for xml path('')) ,1,1,'')
from #A as a
/*
Class Name
0,1,2,3,4,7 苹果,雪梨,香蕉,西瓜,葡萄,石榴
*/