I have a table that contains content (like blog posts, so fairly long text) that I want to export to a xml file.
So I want it like:
<table>
<column1>1231</column1>
<column2>January 1, 2001</column2>
<column3>some very long text will all types of characters in it</column3>
</table>
Is there a built in way to do this?
Basically each column will have its own element.
The content should ideally be CDATA since the content can contain any type of character potentially.
I have sql server 2008 express.
From SQL Server 2005, the FOR XML clause provides a way to convert the results of an SQL query to XML.
E.g.
Consider a table building with Blgd, Suit, SQFT, PDate columns.
will convert the contents of table to the following XML:
If you want the columns to be elements, then
would convert the contents to following XML:
If you want to model your text fields as
CDATAsections, then you should use theFOR XML EXPLICITclause and define your XML schema as per the guidelines here.If the above Building table has a text_col column of type
TEXTthat should be modeled as CDATA section in the generated XML, then theSELECTquery would be as follows:The results would be as follows: