下面是我的代码。
DECLARE @msg NVARCHAR(MAX) = NULL ;WITH CTE AS ( SELECT 'A' AS Message UNION SELECT 'B' AS Message UNION SELECT 'C' AS Message UNION SELECT 'D' AS Message ) SELECT @msg = COALESCE(ISNULL(@msg,'Attachements') + ', ','') + Message FROM CTE SELECT @msg + ' are missing.'
它正在生成输出:-
Attachments, A, B, C, D are missing.
我如何避免在单词“ Attachments”之后出现第一个逗号?请帮忙。
Attachments
满足要求的其他技术也将受到欢迎。
谢谢。
不要使用未记录/不受支持的字符串连接方式,FOR XML PATH而应使用:
FOR XML PATH
DECLARE @msg NVARCHAR(MAX) = NULL ;WITH CTE AS ( SELECT 'A' AS Message UNION SELECT 'B' AS Message UNION SELECT 'C' AS Message UNION SELECT 'D' AS Message ) SELECT @msg = 'Attachments ' + STUFF(( SELECT ', ' + Message FROM CTE ORDER BY Message FOR XML PATH(''), TYPE).value('.[1]', 'NVARCHAR(MAX)') , 1, 2, ' ') SELECT @msg + ' are missing.'