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

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.

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

Saved successfully!

Ooh no, something went wrong!