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

510<br />

OPENXML is a rowset function that opens your string much as other rowset functions (such as OPENQUERY<br />

and OPENROWSET) work. This means that you can join to an XML document, or even use it as the source<br />

of input data by using an INSERT..SELECT or a SELECT INTO. The major difference is that it requires<br />

you to use a couple of system stored procedures to prepare your document and clear the memory after<br />

you’re done using it.<br />

To set up your document, you use sp_xml_preparedocument. This moves the string into memory and<br />

pre-parses it for optimal query performance. The XML document will stay in memory until you explicitly<br />

say to remove it or you terminate the connection that sp_xml_preparedocument was called on. The<br />

syntax is pretty simple:<br />

sp_xml_preparedocument @hdoc = OUTPUT,<br />

[, @xmltext = ]<br />

[, @xpath_namespaces = ]<br />

Note that, if you are going to provide a namespace URL, you need to wrap it in the<br />

< and > symbols at both ends (for example, ).<br />

The parameters of this sproc are fairly self-describing:<br />

❑ @hdoc: If you’ve ever programmed to the Windows API (and to tons of other things, but this is a<br />

common one), then you’ve seen the h before — it’s Hungarian notation for a handle. A handle is<br />

effectively a pointer to a block of memory where something (could be about anything) resides.<br />

In our case, this is the handle to the XML document that we’ve asked <strong>SQL</strong> <strong>Server</strong> to parse and<br />

hold onto for us. This is an output variable — the variable you reference here will, after the<br />

sproc returns, contain the handle to your XML — be sure to store it away, as you will need it<br />

when you make use of OPENXML.<br />

❑ @xmltext: Is what it says it is — the actual XML that you want to parse and work with.<br />

❑ @xpath_namespaces: Any namespace reference(s) your XML needs to operate correctly.<br />

After calling this sproc and saving away the handle to your document, you’re ready to make use of<br />

OPENXML. The syntax for it is slightly more complex:<br />

OPENXML(,[, ])<br />

[WITH (|)]<br />

We have pretty much already discussed the handle — this is going to be an integer value that you<br />

received as an output parameter for your sp_xml_preparedocument call.<br />

When you make your call to OPENXML, you must supply a path to a node that will serve as a starting point<br />

for all your queries. The schema declaration can refer to all parts of the XML document by navigating relative<br />

to the base node you set here.

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

Saved successfully!

Ooh no, something went wrong!