如何让返回的表里的某一列成为一行来返回~
比如:
select top 4 id from [tb]
现返回
1
2
4
6
如何让返回的结果成为
1,2,4,6呢?只是在查询分析器里~
[解决办法]
declare @str varchar(100)
declare @str varchar(100)
set @str = ' '
select top 4 @str = @str + cast(id as varchar) from [tb]
select @str
[解决办法]
declare @str varchar(100)
set @str = ' '
select top 4 @str = @str + cast(id as varchar) from [tb]
select @str
[解决办法]
例如
declare @str varchar(100)
set @str = ' '
select top 4 @str = @str + ', '+ cast(state as varchar) from authors
select stuff(@str,1,1, ' ')
----
CA,CA,CA,CA
[解决办法]
declare @re varchar(200)
set @re= ' '
select top 2 @re=@re+ ', '+rtrim(id) from
(
select id=1
union all select id=2
union all select id=3
union all select id=4
)tmp
select stuff(@re, 1, 1, ' ')
--result
1,2
(1 row(s) affected)
[解决办法]
declare @re varchar(1000)
set @re= ' '
select top 4 @re=@re+ ', '+rtrim(id) from [tb]
select stuff(@re, 1, 1, ' ')