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 />

Check the output from this:<br />

298908<br />

3006712<br />

What it created is a row element for each row in the query — much as we had with RAW — but notice the<br />

difference in how it treated our column data.<br />

Since the CustomerID column was named, it was placed in its own element (we’ll explore this more in our<br />

next section) — notice, however, the number 8 in my results. This is just loose embedded text for the row element<br />

— it isn’t even associated directly with the CustomerID since it is outside the CustomerID element.<br />

I feel like I’m repeating myself for the five thousandth time by saying this, but, again, remember that the<br />

exact counts (4 and 8 in my case) that come back may vary on your system depending on how much<br />

you have been playing with the data in the SalesOrderHeader table. The key thing is to see how the<br />

counts are not associated with the CustomerID, but are instead just raw text associated with the row.<br />

My personal slant on this is that the situations where loose text at the level of the top element is a valid<br />

way of doing things is pretty limited. The rules do say you can do it, but I believe it makes for data that<br />

is not very clear. Still, this is how it works — use it as it seems to fit the needs of your particular system.<br />

Named Columns<br />

This is where things get considerably more complex rather quickly. In their most simple form, named<br />

columns are just as easy as unnamed were — indeed, we saw one of them in our previous example. If a<br />

column is a simple named column using PATH, then it is merely added as an additional element to the row.<br />

@<br />

506<br />

3006712<br />

Our CustomerID column was a simple named column.<br />

We can, however, add special characters into our column name to indicate that we want special behaviors<br />

for this column. Let’s look at a few of the most important.<br />

No, that’s not a typo — the @ symbol is really the heading to this section. If we add an @ sign to our column<br />

name, then <strong>SQL</strong> <strong>Server</strong> will treat that column as an attribute of the previous column. Note that we<br />

also have to delimit the alias in single quotes to hide the @ sign (which is usually an indicator of a variable).<br />

Let’s move the CustomerID to be an attribute of the top element for the row:<br />

SELECT CustomerID AS ‘@CustomerID’, COUNT(*)<br />

FROM Sales.SalesOrderHeader Orders<br />

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

GROUP BY CustomerID<br />

FOR XML PATH;<br />

This yields:<br />

8<br />

12

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

Saved successfully!

Ooh no, something went wrong!