日期:2014-05-18 浏览次数:20563 次
--> 测试数据:[tb] IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb] GO CREATE TABLE [tb]([SubCode] VARCHAR(3),[SbuName] VARCHAR(1)) INSERT [tb] SELECT '001','x' UNION ALL SELECT '002','x' --------------开始查询-------------------------- SELECT DISTINCT SubCode =STUFF((SELECT ','+[SubCode] FROM [tb] WHERE [SbuName]=t.[SbuName] FOR XML PATH('')),1,1,''),[SbuName] FROM [tb] AS t ----------------结果---------------------------- /* SubCode SbuName ------------------------- 001,002 x */
------解决方案--------------------