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

Note that the URL portion of the namespace declaration must be entered on a single line. They are<br />

shown here word wrapped onto multiple lines because there is a limit to the number of characters we<br />

can show per line in print. Make sure you include the entire URL on a single line.<br />

Enforcing Constraints Beyond the Schema Collection<br />

We are, of course, used to the concept of constraints by now. We’ve dealt with them extensively in this<br />

book. Well, if our relational database needs constraints, it follows that our XML data does too. Indeed,<br />

we’ve already implemented much of the idea of constraints in XML through the use of schema collections.<br />

But what if we want to enforce requirements that go beyond the base schema?<br />

Surprisingly, you cannot apply XML data type methods within a constraint declaration. How do you get<br />

around this problem? Well, wrap the tests up in a user-defined function (UDF), and then utilize that<br />

function in your constraint.<br />

I have to admit I’m somewhat surprised that the methods are not usable within the CONSTRAINT declaration,<br />

but things like functions are. All I can say is “go figure. . . .” I’ll just quietly hope they fix this<br />

in a future release, as it seems a significant oversight on something that shouldn’t have been all that difficult<br />

(yeah, I know — easy for me to say since they have to write that code, not me!).<br />

Retrieving Relational Data in XML Format<br />

Retrieving relational data is the area that <strong>SQL</strong> <strong>Server</strong> already had largely figured out even in the older<br />

releases. We had a couple of different options, and we had still more options within those options —<br />

between them all, things have been pretty flexible for quite some time. Let’s take a look . . . .<br />

The FOR XML Clause<br />

498<br />

This clause is at the root of many of the different integration models available. It is essentially just an<br />

option added onto the end of the existing T-<strong>SQL</strong> SELECT statement, but serves as the primary method<br />

for taking data stored in normal relational format and outputting it as XML.<br />

Let’s look at the SELECT statement syntax from Chapter 3:<br />

SELECT [TOP () [PERCENT] [WITH TIES]] <br />

[FROM ]<br />

[WHERE ]<br />

[GROUP BY ]<br />

[HAVING ]<br />

[ORDER BY ]<br />

[[FOR XML {RAW|AUTO|EXPLICIT|PATH [()]}<br />

[, XMLDATA | XMLSCHEMA [()]]<br />

[, ELEMENTS [XSINIL | ABSENT]][, BINARY base 64][ROOT(‘‘)]]<br />

[OPTION (, [, ...n])]<br />

Most of this should seem pretty trivial by now — after all, we’ve been using this syntax throughout a lot<br />

of hard chapters by this time — but it’s time to focus in on that FOR XML line … .

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

Saved successfully!

Ooh no, something went wrong!