把某一列数据转换成指定分隔符的一行数据

来源:互联网 发布:淘宝商城朵以 编辑:程序博客网 时间:2024/06/16 12:52

代码简单,不过多做解释:

if not object_id('tempdb.dbo.#a') is null
  drop table tempdb.dbo.#a
go

--建立测试数据
create table #a( a_type int)
insert #a
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5

--查询
select * from #a

--列转换成行
declare @s varchar(50)
set @s=''
select @s=@s+cast(a_type as varchar(10))+',' from #a
select @s

--执行结果:

图片

原创粉丝点击