I am using the FOR XML PATH
function in order to concatenate multiple columns into a single row. However, I can't figure out how to alias the column name of the resulting table.
Here is the SQL query:
SELECT Comment
FROM Comments
WHERE ID = 1006
FOR XML PATH('')
I have tried the following two methods which generate an error message:
SELECT Comment
FROM Comments
WHERE ID = 1006
FOR XML PATH('') [Comment_Agg];
SELECT * AS Comment_Agg
FROM
(SELECT Comment
FROM Comments
WHERE ID = 1006
FOR XML PATH(''));
FYI, I am using SSMS 18.
3条答案
按热度按时间lztngnrs1#
I googled more and found the resolution as follows:
oxosxuxt2#
To use
FOR XML PATH
to concatenate, the column needs to be unnamed. If you don't want a separator, you can just add+ ''
svgewumm3#
You can use CTE: