Control Freak
Control Freak

Reputation: 13243

Getting result of FOR XML into a variable

   SELECT @Name = Name FROM Table FOR XML AUTO

Does not work, how do you get the XML result from using FOR XML into a variable?

Upvotes: 1

Views: 84

Answers (1)

Yuck
Yuck

Reputation: 50855

This will work:

SELECT @Name = CONVERT(XML, (
  SELECT Name
  FROM SomeTable
  FOR XML AUTO
));

You can try it without the wrapping CONVERT(XML, (...)) statement but I've found that SQL Server doesn't like assigning to XML variables without that explicit cast.

Upvotes: 3

Related Questions