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.

Easy, right? Well, now the boss comes back and says, “Great — now I’ll just have Billy Bob write something<br />

to turn this into XML — too bad that will probably take a day or two.” This is your cue to step in<br />

and say, “Oh, why didn’t you say so?” and simply add three key words:<br />

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

Chapter 16: A Brief XML Primer<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 />

FOR XML RAW;<br />

You have just made the boss very happy. The output is a one-to-one match versus what we would have<br />

seen in the result set had we run just a standard <strong>SQL</strong> query:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Be aware that Management Studio will truncate any column where the length exceeds the number set<br />

in the Options menu in the Results to Text tab (maximum is 8192). This issue exists in the results window<br />

(grid or text) and if you output directly to a file. This is an issue with the tool — not <strong>SQL</strong> <strong>Server</strong><br />

itself. If you use another method to retrieve results (ADO for example), you shouldn’t encounter an<br />

issue with this.<br />

We have one element in XML for each row of data our query produced. All column information, regardless<br />

of what table was the source of the data, is represented as an attribute of the “row” element. The<br />

downside of this is that we haven’t represented the true hierarchical nature of our data — orders are only<br />

placed by customers. The upside, however, is that the XML DOM — if that’s the model you’re using — is<br />

going to be much less deep and, hence, will have a slightly smaller footprint in memory and perform better,<br />

depending on what you’re doing.<br />

501

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

Saved successfully!

Ooh no, something went wrong!