日期:2014-05-18 浏览次数:20533 次
DECLARE @T TABLE ( tid INT ) INSERT INTO @T SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 DECLARE @sql VARCHAR(50) SET @sql='' SELECT @sql=@sql+','+CAST(tid AS VARCHAR(4)) FROM @T PRINT right(@sql,LEN(@sql)-1) /* (所影响的行数为 4 行) 1,2,3,4 */
------解决方案--------------------
create table #Test(id int) insert into #Test select 1 union all select 2 union all select 3 union all select 4 select * from #Test declare @Str varchar(100) set @Str='' select @Str=@Str+','+cast(id as varchar(100)) from #Test select stuff(@Str,1,1,'') //1,2,3,4