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 512 ❑ MetaProperties are a set of special variables that you can refer to in your OPENXML queries. They describe various aspects of whatever part of the XML DOM you’re interested in. To use them, just enclose them in single quotes and put them in the place of the column XPath. Available MetaProperties include: ❑ @mp:id: Don’t confuse this with the XML id that we looked at with EXPLICIT. While this property serves a similar function, it is a unique identifier (within the scope of the document) of the DOM node. The difference is that this value is system generated — as such, you can be sure it is there. It is guaranteed to refer to the same XML node as long as the document remains in memory. If the id is zero, it is the root node (its @mp:parentid property, as referred to below, will be NULL). ❑ @mp:parentid: This is the same as above, only for the parent. ❑ @mp:localname: Provides the non-fully qualified name of the node. It is used with prefix and namespace URI (Uniform Resource Identifier — you’ll usually see it starting with URN) to name element or attribute nodes. ❑ @mp:parentlocalname: This is the same as above, only for the parent. ❑ @mp:namespaceuri: Provides the namespace URI of the current element. If the value of this attribute is NULL, no namespace is present. ❑ @mp:parentnamespaceuri: This is the same as above, only for the parent. ❑ @mp:prefix: Stores the namespace prefix of the current element name. ❑ @mp:prev: Stores the mp:id of the previous sibling relative to a node. Using this, you can tell something about the ordering of the elements at the current level of the hierarchy. For example, if the value of @mp:prev is NULL, then you are at the first node for this level of the tree. ❑ @mp:xmltext: This MetaProperty is used for processing purposes, and contains the actual XML for the current element. Of course, you can always save yourself a ton of work by bypassing all these parameters. You get to do this if you have a table that directly relates (names and data types) to the XPath starting point that you’ve specified in your XML. If you do have such a table, you can just name it and SQL Server will make the translation for you! OK, that’s a lot to handle, but we’re not quite finished yet. You see, when you’re all done with your XML, you need to call sp_xml_removedocument to clean up the memory where your XML document was stored. Thankfully, the syntax is incredibly easy: sp_xml_removedocument [hdoc = ] I can’t stress enough how important it is to get in the habit of always cleaning up after yourself. I know that, in saying that, I probably sound like your mother. Well, like your mother, SQL Server will clean up after you some, but, like your mother, SQL Server won’t clean up after you every time. SQL Server will clean things up when you terminate the connection, but what if you are using connection pooling? Some connections may never go away if your system is under load. It’s an easy sproc to implement, so do it — every time! OK, I’m sure you’ve been bored waiting for me to get to how you really make use of this — so now it’s time for the all-important example.
Imagine that you are merging with another company and need to import some of their data into your system. For this example, we’ll say that we’re working on importing a few shippers that they have and our company doesn’t. For our example, we’re going to import the rows into the Accounting database that we created back in Chapter 5 (you can find the create syntax for the table with the Chapter 5 code — only the Shippers table should be required for this to work). A sample of what our script might look like to import these from an XML document might be: USE Accounting; DECLARE @idoc int; DECLARE @xmldoc nvarchar(4000); -- define the XML document SET @xmldoc = ‘ ‘; --Load and parse the XML document in memory EXEC sp_xml_preparedocument @idoc OUTPUT, @xmldoc; --List out what our shippers table looks like before the insert SELECT * FROM Shippers; -- ShipperID is an IDENTITY column, so we need to allow direct updates SET IDENTITY_INSERT Shippers ON --See our XML data in a tabular format SELECT * FROM OPENXML (@idoc, ‘/ROOT/Shipper’, 0) WITH ( ShipperID int, CompanyName nvarchar(40)); --Perform and insert based on that data INSERT INTO Shippers (ShipperID, ShipperName) SELECT * FROM OPENXML (@idoc, ‘/ROOT/Shipper’, 0) WITH ( ShipperID int, CompanyName nvarchar(40)); --Set things back to normal SET IDENTITY_INSERT Shippers OFF; --Now look at the Shippers table after our insert SELECT * FROM Shippers; --Now clear the XML document from memory EXEC sp_xml_removedocument @idoc; Chapter 16: A Brief XML Primer 513
- 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 548 and 549: Chapter 16: A Brief XML Primer 510
- 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
- Page 598 and 599: Chapter 18: Getting Integrated with
Chapter 16: A Brief XML Primer<br />
512<br />
❑ MetaProperties are a set of special variables that you can refer to in your OPENXML queries. They<br />
describe various aspects of whatever part of the XML DOM you’re interested in. To use them,<br />
just enclose them in single quotes and put them in the place of the column XPath. Available<br />
MetaProperties include:<br />
❑ @mp:id: Don’t confuse this with the XML id that we looked at with EXPLICIT. While<br />
this property serves a similar function, it is a unique identifier (within the scope of the<br />
document) of the DOM node. The difference is that this value is system generated — as<br />
such, you can be sure it is there. It is guaranteed to refer to the same XML node as long<br />
as the document remains in memory. If the id is zero, it is the root node (its<br />
@mp:parentid property, as referred to below, will be NULL).<br />
❑ @mp:parentid: This is the same as above, only for the parent.<br />
❑ @mp:localname: Provides the non-fully qualified name of the node. It is used with prefix<br />
and namespace URI (Uniform Resource Identifier — you’ll usually see it starting<br />
with URN) to name element or attribute nodes.<br />
❑ @mp:parentlocalname: This is the same as above, only for the parent.<br />
❑ @mp:namespaceuri: Provides the namespace URI of the current element. If the value of<br />
this attribute is NULL, no namespace is present.<br />
❑ @mp:parentnamespaceuri: This is the same as above, only for the parent.<br />
❑ @mp:prefix: Stores the namespace prefix of the current element name.<br />
❑ @mp:prev: Stores the mp:id of the previous sibling relative to a node. Using this, you can tell<br />
something about the ordering of the elements at the current level of the hierarchy. For example,<br />
if the value of @mp:prev is NULL, then you are at the first node for this level of the tree.<br />
❑ @mp:xmltext: This MetaProperty is used for processing purposes, and contains the<br />
actual XML for the current element.<br />
Of course, you can always save yourself a ton of work by bypassing all these parameters. You get to do<br />
this if you have a table that directly relates (names and data types) to the XPath starting point that<br />
you’ve specified in your XML. If you do have such a table, you can just name it and <strong>SQL</strong> <strong>Server</strong> will<br />
make the translation for you!<br />
OK, that’s a lot to handle, but we’re not quite finished yet. You see, when you’re all done with your<br />
XML, you need to call sp_xml_removedocument to clean up the memory where your XML document<br />
was stored. Thankfully, the syntax is incredibly easy:<br />
sp_xml_removedocument [hdoc = ]<br />
I can’t stress enough how important it is to get in the habit of always cleaning up after yourself. I know that,<br />
in saying that, I probably sound like your mother. Well, like your mother, <strong>SQL</strong> <strong>Server</strong> will clean up after you<br />
some, but, like your mother, <strong>SQL</strong> <strong>Server</strong> won’t clean up after you every time. <strong>SQL</strong> <strong>Server</strong> will clean things<br />
up when you terminate the connection, but what if you are using connection pooling? Some connections may<br />
never go away if your system is under load. It’s an easy sproc to implement, so do it — every time!<br />
OK, I’m sure you’ve been bored waiting for me to get to how you really make use of this — so now it’s<br />
time for the all-important example.