数据列合并 --(1) 串字符 /* 案例: 循环某一列记录,串成一个字符串 */ --测试环境 DECLARE @T TABLE(ID INT IDENTITY(1,1),BrandName VARCHAR(20)) INSERT INTO @T (BrandName) SELECT '荷氏' UNION ALL SELECT '吉百利巧克力' UNION ALL SELECT '其他品牌' UNION ALL SELECT '巧心玉' UNION ALL SELECT '维果C' UNION ALL SELECT '怡口莲' --(1) sql 2000 DECLARE @Result VARCHAR(2000) SET @Result='' SELECT @Result=@Result+','+BrandName FROM @T SELECT @Result AS BrandName --(2) SQL 2005 --a、方法一: SELECT BrandName= STUFF(REPLACE(REPLACE( ( SELECT BrandName FROM @t N FOR XML AUTO ), '<N BrandName="', ','), '"/>', ''), 1, 1, '') --b、方法二: select stuff((select distinct ','+BrandName from @t for xml path('')),1,1,'')as BrandName