How does the FOR XML PATH ('')
clause do its work when concatenating rows in SQL Server?
I just want an explanation of how the FOR XML PATH ('')
clause works...
How does the FOR XML PATH ('')
clause do its work when concatenating rows in SQL Server?
I just want an explanation of how the FOR XML PATH ('')
clause works...
What
FOR XML PATH('xxx')
does is create an XML string for the resultset that puts each row in a<xxx></xxx>
element and each column value inside the row, in an element with the name for that column.If the PATH is empty (i.e.
PATH('')
) it omits the row element in the XML generation. If the column has no name it omits the column element in the XML generation. When both PATH is empty and columns have no names it effectively becomes a string concatenation of all rows.Run the following statements to get a better insight in the process:
Now I hear you asking: How can I remove the column name when I simply select a column from a table. There are several ways, in order of my preference:
SELECT [text()]=column_name ...
SELECT (SELECT column_name) ...
SELECT CAST(column_value AS <TYPE of the column>) ...
Examples: