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.

Next up are the mapping flags. These assist you in deciding whether you want to favor elements or<br />

attributes in your OPENXML results. The options are:<br />

Byte Value Description<br />

Chapter 16: A Brief XML Primer<br />

0 Same as 1 except that you can’t combine it with 2 or 8 (2 + 0 is still 2). This is the<br />

default.<br />

1 Unless combined with 2 below, only attributes will be used. If there is no attribute<br />

with the name specified, then a NULL is returned. This can also be added to either 2<br />

or 8 (or both) to combine behavior, but this option takes precedence over option 2. If<br />

XPath finds both an attribute and an element with the same name, the attribute wins.<br />

2 Unless combined with 1 above, only elements will be used. If there is no element<br />

with the name specified, then a NULL is returned. This can also be added to either 1<br />

or 8 (or both) to combine behavior. If combined with 1, then the attribute will be<br />

mapped if it exists. If no attribute exists, then the element will be used. If no element<br />

exists, then a NULL is returned.<br />

8 Can be combined with 1 or 2 above. Consumed data should not be copied to the<br />

overflow property @mp:xmltext (you would have to use the MetaProperty<br />

schema item to retrieve this). If you’re not going to use the MetaProperties — and<br />

most of the time you won’t be — I recommend this option. It cuts a small (OK, very<br />

small) amount of overhead out of the operation.<br />

Finally comes the schema or table. If you’re defining a schema and are not familiar with XPath, this part<br />

can be a bit tricky. Fortunately, this particular XPath use isn’t very complex and should become second<br />

nature fairly quickly (it works a lot like directories do in Windows).<br />

The schema can vary somewhat in the way you declare it. The definition is declared as:<br />

WITH (<br />

[{|}]<br />

[, [{|}]<br />

...<br />

❑ The column name is just that — the name of the attribute or element you are retrieving. This will<br />

also serve as the name you refer to when you build your SELECT list, perform JOINs, and so on.<br />

❑ The data type is any valid <strong>SQL</strong> <strong>Server</strong> data type. Because XML can have data types that are not<br />

equivalents of those in <strong>SQL</strong> <strong>Server</strong>, an automatic coercion will take place if necessary, but this is<br />

usually predictable.<br />

❑ The column XPath is the XPath pattern (relative to the node you established as the starting<br />

point for your OPENXML function) that gets you to the node you want for your column — whether<br />

an element or attribute gets used is dependent on the flags parameter as described earlier. If this<br />

is left off, then <strong>SQL</strong> <strong>Server</strong> assumes you want the current node as defined as the starting point<br />

for your OPENXML statement.<br />

511

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

Saved successfully!

Ooh no, something went wrong!