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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 16: A Brief XML Primer<br />

AUTO<br />

502<br />

AUTO takes a somewhat different approach to our data than RAW does. AUTO tries to format things a little<br />

better for us — naming elements based on the table (or the table alias if you use one). In addition, AUTO<br />

recognizes the notion that our data probably has some underlying hierarchical notion to it that is supposed<br />

to be represented in the XML.<br />

Let’s go back to our customer orders example from the last section. This time, we’ll make use of the AUTO<br />

option, so we can see the difference versus the rather plain output we got with RAW. We’ll also make<br />

extensive use of aliasing to make our elements have more realistic names:<br />

USE AdventureWorks<strong>2008</strong>;<br />

SELECT Customer.CustomerID,<br />

Customer.AccountNumber,<br />

[Order].SalesOrderID,<br />

CAST([Order].OrderDate AS date) AS OrderDate<br />

FROM Sales.Customer Customer<br />

JOIN Sales.SalesOrderHeader [Order]<br />

ON Customer.CustomerID = [Order].CustomerID<br />

WHERE Customer.CustomerID = 29890 OR Customer.CustomerID = 30067<br />

FOR XML AUTO;<br />

The first apparent difference is that the element name has changed to the name or alias of the table that<br />

is the source of the data. Notice also that I was able to output XML that included the <strong>SQL</strong> <strong>Server</strong> keyword<br />

Order by delimiting it in square brackets. Another even more significant difference appears when we<br />

look at the XML more thoroughly (I have again cleaned up the output a bit for clarity):<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Data that is sourced from our second table (as determined by the SELECT list) is nested inside the data<br />

sourced from the first table. In this case, our Order elements are nested inside our Customer elements. If a<br />

column from the Order table were listed first in our select list, then Customer would be nested inside Order.<br />

Pay attention to this business of the ordering of your SELECT list. Think about the primary question your<br />

XML query is meant to solve. Arrange your SELECT list such that the style that it produces is fitting for<br />

the goal of your XML. Sure, you could always re-style it into the different form — but why do that if <strong>SQL</strong><br />

<strong>Server</strong> could have just produced it for you that way in the first place?

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

Saved successfully!

Ooh no, something went wrong!