日期:2014-05-17  浏览次数:20342 次

select出的数据放到一个变量里读取要怎么写,多谢
id 
1
2
3
4
5
6
7

select id from xxx,读出的是多条,要放到sqlserver的一个变量里,变成1,2,3,4,5,6,7然后再读出来,要怎么弄,多谢

------解决方案--------------------


declare @t table (id int)
declare @s varchar(128)=''
insert into @t
select 1 union all select 2 union all select 3

select @s =@s+CONVERT(varchar(8),id)+',' from @t
select @s as S
--------------------
(3 行受影响)
S
-------------
1,2,3,

(1 行受影响)


------解决方案--------------------

--> 测试数据: @T
declare @T table (id int)
insert into @T
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7
declare @sql varchar(200)
select @sql=isnull(@sql+',','')+ltrim(id) from @T
select @sql
/*
1,2,3,4,5,6,7
*/