两种方式,效率立竿见影
------------------------------------------------ SET STATISTICS TIME ON DECLARE @OrderStr NVARCHAR(max) = ''; SELECT @OrderStr += a.SerialNumber+',' FROM dbo.WithdrawalsLog a SET STATISTICS TIME OFF ------------------------------------------------ SET STATISTICS TIME ON DECLARE @OrderStr NVARCHAR(max) = ''; SET @OrderStr = ( SELECT a.SerialNumber+',' FROM dbo.WithdrawalsLog a FOR XML PATH('') ) SET STATISTICS TIME OFF ------------------------------------------------
如果要获取某个表的字段,并把它拼接成一个字符串,那这个方法再好不过了: SELECT bankcode+','+bankname+';' FROM BankInfo FOR XML PATH('')
懒得写了,看不懂的看这里:
http://www.cnblogs.com/doubleliang/archive/2011/07/06/2098775.html