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.

Chapter 16: A Brief XML Primer<br />

RAW<br />

500<br />

This is something of the “no fuss, no muss” option. The idea here is to just get it done — no fanfare, no<br />

special formatting at all — just the absolute minimum to translate a row of relational data into an element<br />

of XML data. The element is named “row” (creative, huh?), and each column in the Select list is<br />

added as an attribute using whatever name the column would have appeared with, if you had been running<br />

a more traditional SELECT statement.<br />

One downside to the way in which attributes are named is that you need to make certain that every column<br />

has a name. Normally, <strong>SQL</strong> <strong>Server</strong> will just show no column heading if you perform an aggregation<br />

or other calculated column and don’t provide an alias — when doing XML queries, everything<br />

MUST have a name, so don’t forget to alias calculated columns.<br />

So, let’s start things out with something relatively simple. Imagine that our manager has asked us to provide<br />

a query that lists a few customers’ orders — say CustomerIDs 1 and 2. After cruising through just the<br />

first five or so chapters of the book, you would probably say “No problem!” and supply something like:<br />

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

SELECT Sales.Customer.CustomerID,<br />

Sales.Customer.AccountNumber,<br />

Sales.SalesOrderHeader.SalesOrderID,<br />

CAST(Sales.SalesOrderHeader.OrderDate AS date) AS OrderDate<br />

FROM Sales.Customer<br />

JOIN Sales.SalesOrderHeader<br />

ON Sales.Customer.CustomerID = Sales.SalesOrderHeader.CustomerID<br />

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

So, you go hand your boss the results:<br />

CustomerID AccountNumber SalesOrderID OrderDate<br />

----------- ------------- ------------ ----------<br />

29890 AW00029890 43671 <strong>2008</strong>-08-16<br />

29890 AW00029890 45049 <strong>2008</strong>-08-16<br />

29890 AW00029890 45790 <strong>2008</strong>-08-16<br />

29890 AW00029890 46619 <strong>2008</strong>-08-16<br />

29890 AW00029890 47672 <strong>2008</strong>-08-16<br />

29890 AW00029890 48732 <strong>2008</strong>-08-16<br />

29890 AW00029890 49866 <strong>2008</strong>-08-16<br />

29890 AW00029890 61187 <strong>2008</strong>-08-16<br />

30067 AW00030067 43672 <strong>2008</strong>-08-16<br />

30067 AW00030067 44294 <strong>2008</strong>-08-16<br />

30067 AW00030067 45052 <strong>2008</strong>-08-16<br />

30067 AW00030067 45792 <strong>2008</strong>-08-16<br />

30067 AW00030067 46622 <strong>2008</strong>-08-16<br />

30067 AW00030067 47673 <strong>2008</strong>-08-16<br />

30067 AW00030067 48768 <strong>2008</strong>-08-16<br />

30067 AW00030067 49860 <strong>2008</strong>-08-16<br />

30067 AW00030067 51100 <strong>2008</strong>-08-16<br />

30067 AW00030067 55287 <strong>2008</strong>-08-16<br />

30067 AW00030067 61222 <strong>2008</strong>-08-16<br />

30067 AW00030067 67272 <strong>2008</strong>-08-16<br />

(20 row(s) affected)

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

Saved successfully!

Ooh no, something went wrong!