将数据库中某个字段重叠后以字符串形式返回-摘记

时间:2021-03-08 15:11:48

1、

declare @sql as varchar(8000)

select @sql=''

Select @sql=@sql+UserName【字段名】+','

from dbo.b_NewGrant【表名】

select @sql

 

2、

 

declare @mytable table (Col1 varchar(10))
insert into @mytable values ('AA'),('BB'),('CC') 

declare @Colstring varchar(50)
select @Colstring = isnull(@Colstring+',' , '' ) + isnull( Col1 , '' ) from @mytable 
select @Colstring as Colstring