SELECT Id, Names = (STUFF((SELECT ', ' + Names FROM Table WHERE Id = d.Id FOR xml path('') ),1,1,'')) FROM Table d GROUP by Id
另外:
sql 单引号转义:两个单引号转义为一个单引号
set @sql='STUFF((SELECT '','' + Names FROM Table WHERE Id = d.Id FOR xml path('''') ),1,1,'''')) FROM Table d GROUP by Id '
'' 为 '