Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 16: A Brief XML Primer 510 OPENXML is a rowset function that opens your string much as other rowset functions (such as OPENQUERY and OPENROWSET) work. This means that you can join to an XML document, or even use it as the source of input data by using an INSERT..SELECT or a SELECT INTO. The major difference is that it requires you to use a couple of system stored procedures to prepare your document and clear the memory after you’re done using it. To set up your document, you use sp_xml_preparedocument. This moves the string into memory and pre-parses it for optimal query performance. The XML document will stay in memory until you explicitly say to remove it or you terminate the connection that sp_xml_preparedocument was called on. The syntax is pretty simple: sp_xml_preparedocument @hdoc = OUTPUT, [, @xmltext = ] [, @xpath_namespaces = ] Note that, if you are going to provide a namespace URL, you need to wrap it in the < and > symbols at both ends (for example, ). The parameters of this sproc are fairly self-describing: ❑ @hdoc: If you’ve ever programmed to the Windows API (and to tons of other things, but this is a common one), then you’ve seen the h before — it’s Hungarian notation for a handle. A handle is effectively a pointer to a block of memory where something (could be about anything) resides. In our case, this is the handle to the XML document that we’ve asked SQL Server to parse and hold onto for us. This is an output variable — the variable you reference here will, after the sproc returns, contain the handle to your XML — be sure to store it away, as you will need it when you make use of OPENXML. ❑ @xmltext: Is what it says it is — the actual XML that you want to parse and work with. ❑ @xpath_namespaces: Any namespace reference(s) your XML needs to operate correctly. After calling this sproc and saving away the handle to your document, you’re ready to make use of OPENXML. The syntax for it is slightly more complex: OPENXML(,[, ]) [WITH (|)] We have pretty much already discussed the handle — this is going to be an integer value that you received as an output parameter for your sp_xml_preparedocument call. When you make your call to OPENXML, you must supply a path to a node that will serve as a starting point for all your queries. The schema declaration can refer to all parts of the XML document by navigating relative to the base node you set here.

Next up are the mapping flags. These assist you in deciding whether you want to favor elements or attributes in your OPENXML results. The options are: Byte Value Description Chapter 16: A Brief XML Primer 0 Same as 1 except that you can’t combine it with 2 or 8 (2 + 0 is still 2). This is the default. 1 Unless combined with 2 below, only attributes will be used. If there is no attribute with the name specified, then a NULL is returned. This can also be added to either 2 or 8 (or both) to combine behavior, but this option takes precedence over option 2. If XPath finds both an attribute and an element with the same name, the attribute wins. 2 Unless combined with 1 above, only elements will be used. If there is no element with the name specified, then a NULL is returned. This can also be added to either 1 or 8 (or both) to combine behavior. If combined with 1, then the attribute will be mapped if it exists. If no attribute exists, then the element will be used. If no element exists, then a NULL is returned. 8 Can be combined with 1 or 2 above. Consumed data should not be copied to the overflow property @mp:xmltext (you would have to use the MetaProperty schema item to retrieve this). If you’re not going to use the MetaProperties — and most of the time you won’t be — I recommend this option. It cuts a small (OK, very small) amount of overhead out of the operation. Finally comes the schema or table. If you’re defining a schema and are not familiar with XPath, this part can be a bit tricky. Fortunately, this particular XPath use isn’t very complex and should become second nature fairly quickly (it works a lot like directories do in Windows). The schema can vary somewhat in the way you declare it. The definition is declared as: WITH ( [{|}] [, [{|}] ... ❑ The column name is just that — the name of the attribute or element you are retrieving. This will also serve as the name you refer to when you build your SELECT list, perform JOINs, and so on. ❑ The data type is any valid SQL Server data type. Because XML can have data types that are not equivalents of those in SQL Server, an automatic coercion will take place if necessary, but this is usually predictable. ❑ The column XPath is the XPath pattern (relative to the node you established as the starting point for your OPENXML function) that gets you to the node you want for your column — whether an element or attribute gets used is dependent on the flags parameter as described earlier. If this is left off, then SQL Server assumes you want the current node as defined as the starting point for your OPENXML statement. 511

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!