2
我有以下表为什么SQL Server会忽略在字符串连接vaules
Created Comment
2010/10/10 Text1
2010/11/11 Text2
2010/12/12 Text3
我需要收集所有的意见纳入单一的字符串
SELECT @Comment = COALESCE(@Comment, '')
+ CHAR(13) + CHAR(10) + CONVERT(NVARCHAR(30), [dbo].[Comment].[Created], 101) + ': ' + ISNULL([Comment].[Text], '')
FROM Comment
没有命令其按预期工作结束返回所有你的评论。 但运行下面的代码,添加ORDER BY子句后:
SELECT @Comment = COALESCE(@Comment, '')
+ CHAR(13) + CHAR(10) + CONVERT(NVARCHAR(30), [Created], 101) + ': ' + ISNULL([Text], '')
FROM Comment
ORDER BY Created
只返回最后一个注释。 有没有人知道为什么ORDER BY导致连接中的奇怪结果?
PS:它工作正常,如果FOR XML子句,而不是拼接Is there a way to create a SQL Server function to “join” multiple rows from a subquery into a single delimited field?。