将表名为 TableName 字段为 Name 的字段的值拼接起来,返回结果 name1,name2,name...
DECLARE @sql varchar(50)
SET @sql = ''
SELECT @sql = @sql + ',' + cast(Name AS varchar)
FROM TableName
WHERE Id = @id RETURN stuff(@sql, 1, 1, '') END
select @sql
方法二:
select aa=stuff(( select top 5 '|'+ckcode+','+rtrim(ckmc) from dbo.zzbz_ckdjb for xml path('')),1,1,'')