Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
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
- Page 498 and 499: Chapter 15: Triggers 460 IF EXISTS
- Page 500 and 501: Chapter 15: Triggers ❑ Feeding de
- Page 502 and 503: Chapter 15: Triggers Trigger Firing
- Page 504 and 505: Chapter 15: Triggers Like regular t
- Page 506 and 507: Chapter 15: Triggers The COLUMNS_UP
- Page 508 and 509: Chapter 15: Triggers This is the sa
- Page 510 and 511: Chapter 15: Triggers 472 we have th
- Page 512 and 513: Chapter 16: A Brief XML Primer So,
- Page 514 and 515: Chapter 16: A Brief XML Primer Figu
- Page 516 and 517: Chapter 16: A Brief XML Primer Elem
- Page 518 and 519: Chapter 16: A Brief XML Primer ❑
- Page 520 and 521: Chapter 16: A Brief XML Primer 482
- Page 522 and 523: Chapter 16: A Brief XML Primer Name
- Page 524 and 525: Chapter 16: A Brief XML Primer The
- Page 526 and 527: Chapter 16: A Brief XML Primer 2. W
- Page 528 and 529: Chapter 16: A Brief XML Primer SQL
- Page 530 and 531: Chapter 16: A Brief XML Primer So,
- Page 532 and 533: Chapter 16: A Brief XML Primer If,
- Page 534 and 535: Chapter 16: A Brief XML Primer 496
- Page 536 and 537: Chapter 16: A Brief XML Primer Note
- Page 538 and 539: Chapter 16: A Brief XML Primer RAW
- Page 540 and 541: Chapter 16: A Brief XML Primer AUTO
- Page 542 and 543: Chapter 16: A Brief XML Primer EXPL
- Page 544 and 545: Chapter 16: A Brief XML Primer Chec
- Page 546 and 547: Chapter 16: A Brief XML Primer 508
- Page 550 and 551: Chapter 16: A Brief XML Primer 512
- Page 552 and 553: Chapter 16: A Brief XML Primer The
- Page 554 and 555: Chapter 16: A Brief XML Primer 516
- Page 556 and 557: Chapter 17: Reporting for Duty, Sir
- Page 558 and 559: Chapter 17: Reporting for Duty, Sir
- Page 560 and 561: Chapter 17: Reporting for Duty, Sir
- Page 562 and 563: Chapter 17: Reporting for Duty, Sir
- Page 564 and 565: Chapter 17: Reporting for Duty, Sir
- Page 566 and 567: Chapter 17: Reporting for Duty, Sir
- Page 568 and 569: Chapter 17: Reporting for Duty, Sir
- Page 570 and 571: Chapter 17: Reporting for Duty, Sir
- Page 572 and 573: Chapter 17: Reporting for Duty, Sir
- Page 574 and 575: Chapter 17: Reporting for Duty, Sir
- Page 576 and 577: Chapter 17: Reporting for Duty, Sir
- Page 578 and 579: Chapter 17: Reporting for Duty, Sir
- Page 580 and 581: Chapter 17: Reporting for Duty, Sir
- Page 582 and 583: Chapter 18: Getting Integrated with
- Page 584 and 585: Chapter 18: Getting Integrated with
- Page 586 and 587: Chapter 18: Getting Integrated with
- Page 588 and 589: Chapter 18: Getting Integrated with
- Page 590 and 591: Chapter 18: Getting Integrated with
- Page 592 and 593: Chapter 18: Getting Integrated with
- Page 594 and 595: Chapter 18: Getting Integrated with
- Page 596 and 597: Chapter 18: Getting Integrated with
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