17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

FOR XML provides three different initial options for how you want your XML formatted in the results:<br />

❑ RAW: This sends each row of data in your result set back as a single data element, with the element<br />

name of “row” and with each column listed as an attribute of the “row” element. Even if<br />

you join multiple tables, RAW outputs the results with the same number of elements as you<br />

would have rows in a standard <strong>SQL</strong> query.<br />

❑ AUTO: This option labels each element with the table name or alias that represents the source of<br />

the data. If there is data output from more than one table in the query, the data from each table<br />

is split into separate, nested elements. If AUTO is used, then an additional option, ELEMENTS, is<br />

also supported if you would like column data presented as elements rather than as attributes.<br />

❑ EXPLICIT: This one is certainly the most complex to format your query with, but the end result<br />

is that you have a high degree of control of what the XML looks like in the end. With this option,<br />

you impose something of a hierarchy for the data that’s being returned, and then format your<br />

query such that each piece of data belongs to a specific hierarchy level (and gets assigned a tag<br />

accordingly) as desired. This choice has largely been supplanted by the PATH option, and is here<br />

primarily for backward compatibility.<br />

❑ PATH: This was added to try and provide the level of flexibility of EXPLICIT in a more usable<br />

format — this is generally going to be what you want to use when you need a high degree of<br />

control of the format of the output.<br />

Note that none of these options provide the required root element. If you want the XML document to be<br />

considered to be well formed, then you will need to wrap the results with proper opening and closing<br />

tags for your root element or have <strong>SQL</strong> <strong>Server</strong> do it for you (using the ROOT option described later).<br />

While this is in some ways a hassle, it is also a benefit — it means that you can build more complex<br />

XML by stringing multiple XML queries together and wrapping the different results into one XML file.<br />

In addition to the four major formatting options, there are other optional parameters that further modify<br />

the output that <strong>SQL</strong> <strong>Server</strong> provides in an XML query:<br />

❑ XMLDATA/XMLSCHEMA: These tell <strong>SQL</strong> <strong>Server</strong> that you would like to prepend one of two forms of<br />

an XML schema to the results. XMLDATA works under the older XDR format, which was common<br />

before the W3C finalized the spec for XML schema documents. You’ll want to use<br />

XMLSCHEMA here unless you have a very specific reason for using the older XDR format, as the<br />

XMLDATA option is provided only for backward compatibility and does not support newer data<br />

types added in <strong>SQL</strong> <strong>Server</strong> 2005 and <strong>2008</strong>.<br />

❑ ELEMENTS: This option is available only when you are using the AUTO formatting option. It tells <strong>SQL</strong><br />

<strong>Server</strong> that you want the columns in your data returned as nested elements rather than as attributes.<br />

❑ BINARY BASE64: This tells <strong>SQL</strong> <strong>Server</strong> to encode any binary columns (binary, varbinary, image)<br />

in base64 format. This option is implied (<strong>SQL</strong> <strong>Server</strong> will use it even if you don’t state it) if you<br />

are also using the AUTO option. It is required when using EXPLICIT and RAW queries.<br />

❑ TYPE: Tells <strong>SQL</strong> <strong>Server</strong> to return the results reporting the XML data type instead of the default<br />

Unicode character type.<br />

❑ ROOT: This option will have <strong>SQL</strong> <strong>Server</strong> add the root node for you so you don’t have to. You can<br />

either supply a name for your root or use the default (root).<br />

Let’s explore all these options in a little more detail.<br />

Chapter 16: A Brief XML Primer<br />

499

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!