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

So, to get rid of our ProductDescriptionSchemaCollectionSummaryRequired schema collection we<br />

created earlier, we could execute:<br />

DROP XML SCHEMA COLLECTION ProductDescriptionSchemaCollectionSummaryRequired;<br />

And it’s gone.<br />

XML Data Type Methods<br />

The XML data type carries several intrinsic methods with it. These methods are unique to the XML data<br />

type, and no other current data type has anything that is at all similar. The syntax within these methods<br />

varies a bit because they are based on different, but mostly industry-standard, XML access methods. The<br />

basic syntax for calling the method is standardized though:<br />

.<br />

There are a total of five methods available:<br />

❑ .query: An implementation of the industry-standard XQuery language. This allows you to<br />

access your XML by running XQuery-formatted queries. XQuery allows for the prospect that<br />

you may be returning multiple pieces of data rather than a discrete value.<br />

❑ .value: This one allows you to access a discrete value within a specific element or attribute.<br />

❑ .modify: This is <strong>Microsoft</strong>’s own extension to XQuery. Whereas XQuery is limited to requesting<br />

data (no modification language), the modify method extends XQuery to allow for data modification.<br />

❑ .nodes: Used to break up XML data into individual, more relational-style rows.<br />

❑ .exist: Much like the IF EXISTS clause we use extensively in standard <strong>SQL</strong>, the exist()<br />

XML data type method tests to see whether a specific kind of data exists. In the case of exist(),<br />

the test is to see whether a particular node or attribute has an entry in the instance of XML<br />

you’re testing.<br />

.query (<strong>SQL</strong> <strong>Server</strong>’s Implementation of XQuery)<br />

492<br />

.query is an implementation of the industry standard XQuery language. The result works much like a<br />

<strong>SQL</strong> query, except that the results are for matching XML data nodes rather than relational rows and<br />

columns.<br />

.query requires a parameter that is a valid XQuery to be run against your instance of XML data. For example,<br />

if we wanted the steps out of the product documentation for ProductID 66, we could run the following:<br />

SELECT ProductModelID, Instructions.query(‘declare namespace PI=”http://<br />

schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelManuInstructions”;<br />

/PI:root/PI:Location/PI:step’) AS Steps<br />

FROM Production.ProductModel<br />

WHERE ProductModelID = 66;<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.

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

Saved successfully!

Ooh no, something went wrong!