The query for this is very simple. Say, our table has two columns,
UserId and UserName. And we want a list of usernames as comma-separated
string, then the query would be
SELECT UserName + ‘,’
FROM TableName
FOR XML PATH(‘ ‘)
The “FOR XML PATH(‘some path’)” returns the result in the form of xml with each select result surrounded by xml tags named “some path”.
For example, if the parameter in “for xml path” is “response”,
the query would be
“SELECT UserName FROM TableName FOR XML PATH(‘response’).
The result of the query would be
<response>UserName1</response>
<response>UserName2</response>
<response>UserName3</response>
<response>UserName4</response>
<response>UserName5</response>
and so on,…………
So, in our case, because nothing is mentioned as parameter to the FOR XML PATH(‘ ‘) function (not even space), the result of the query would be
UserName1, UserName2, UserName3, UserName4, ……….. and so on…
You have your comma-separated result.
SELECT UserName + ‘,’
FROM TableName
FOR XML PATH(‘ ‘)
The “FOR XML PATH(‘some path’)” returns the result in the form of xml with each select result surrounded by xml tags named “some path”.
For example, if the parameter in “for xml path” is “response”,
the query would be
“SELECT UserName FROM TableName FOR XML PATH(‘response’).
The result of the query would be
<response>UserName1</response>
<response>UserName2</response>
<response>UserName3</response>
<response>UserName4</response>
<response>UserName5</response>
and so on,…………
So, in our case, because nothing is mentioned as parameter to the FOR XML PATH(‘ ‘) function (not even space), the result of the query would be
UserName1, UserName2, UserName3, UserName4, ……….. and so on…
You have your comma-separated result.
0 comments:
Post a Comment