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.

FROM Sales.SalesOrderHeader Orders<br />

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

GROUP BY CustomerID<br />

FOR XML PATH;<br />

This probably seems counterintuitive, but, again, think of the order things are being written in. The<br />

attributes are written first, and then, and only then, can we write the lower-level information for the<br />

CustomerID element. Run it, and you’ll see we get what we were after:<br />

29890<br />

30067<br />

OrderCount has now been moved into the attribute position just as we desired, and the actual CustomerID<br />

is still raw text embedded in the element.<br />

Follow the logic of the ordering of what you ask for a bit, because it works for most everything. So, if we<br />

wanted CustomerID to also be an attribute rather than raw text, but wanted it to be after Order-<br />

Count, we can do that — we just need to make sure that it comes after the OrderCount definition.<br />

But Wait, There’s More . . .<br />

As I said earlier, XPath has its own complexity and is a book’s worth to itself, but I don’t want to leave<br />

you with just the preceding text and say that’s all there is.<br />

@ and / will give you a great deal of flexibility in building the XML output just the way you want it, and<br />

probably meet the need well for most beginning applications. If, however, you need something more,<br />

then there is still more out there waiting for you. For example, you can:<br />

❑ “Wildcard” data such that it’s all run together as text data without being treated as separate columns.<br />

❑ Embed native XML data from XML data type columns.<br />

❑ Use XPath node tests — these are special XPath directives that change the behavior of your data.<br />

❑ Use the data() directive to allow multiple values to be run together as one data point in the XML.<br />

❑ Utilize namespaces.<br />

OPENXML<br />

Chapter 16: A Brief XML Primer<br />

Many of the concepts we’ve covered in this chapter up to this point stray towards what I would call<br />

advanced <strong>SQL</strong> <strong>Server</strong> topics. OPENXML strays even farther, and thus we will not delve too deep into it<br />

here. I do, however, want to make sure you understand what it does and some of the situations it can be<br />

useful for. Keep in mind that many of the things OPENXML was created for are now handled in a more<br />

native way by simply placing your XML into a native XML data type and using the XML type methods<br />

we discussed earlier in the chapter.<br />

When the original XML feature set was first introduced back in <strong>SQL</strong> <strong>Server</strong> 2000, the native XML data<br />

type did not yet exist. We had FOR XML, and thus significant power for turning relational data into XML,<br />

but we needed something to make XML addressable in a relational formal — that something was OPENXML.<br />

509

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

Saved successfully!

Ooh no, something went wrong!