日期:2014-05-18 浏览次数:20493 次
SELECT STUFF((SELECT ','+ PageSize FROM TB WHERE FOR XML PATH('')),1,1,'') AS PageSize
------解决方案--------------------
declare @s nvarchar(2000) select @s=isnull(@s+',','')+rtrim(PageSize) from Tab select @s as PageSize
------解决方案--------------------
SELECT STUFF((SELECT ','+ PageSize FROM TB FOR XML PATH('')),1,1,'') AS PageSize
------解决方案--------------------
use Tempdb go --> --> if not object_id(N'Tempdb..#') is null drop table # Go Create table #([PageSize] int) Insert # select 9 union all select 1 union all select 8 Go declare @s nvarchar(2000) select @s=isnull(@s+',','')+rtrim(PageSize) from # select @s as PageSize /* 9,1,8 */
------解决方案--------------------
/*如何将一列中所有的值一行显示 数据源 a b c d e 结果 a,b,c,d,e */ create table tb(col varchar(20)) insert tb values ('a') insert tb values ('b') insert tb values ('c') insert tb values ('d') insert tb values ('e') go --方法一 declare @sql varchar(1000) set @sql = '' select @sql = @sql + t.col + ',' from (select col from tb) as t set @sql='select result = ''' + left(@sql , len(@sql) - 1) + '''' exec(@sql) /* result ---------- a,b,c,d,e, */ --方法二 declare @output varchar(8000) select @output = coalesce(@output + ',' , '') + col from tb print @output /* a,b,c,d,e */ drop table tb
------解决方案--------------------
SELECT STUFF(REPLACE(REPLACE((SELECT pagesize FROM tb FOR XML AUTO),'<tb pagesize="',','),'"/>',''),1,1,'')
------解决方案--------------------
SELECT STUFF((SELECT ','+ PageSize FROM TB FOR XML PATH('')),1,1,'') AS PageSize