T-SQL Query- how to explode XML fragment stored as NText into table?

5 pts.
Tags:
Query
SQL Query
SQL Server 2005
T-SQL
XML
I'm using a commercial storefront software that offers "extension data" fields as NText to store custom data. I am storing an xml fragment that looks like this:
<priceSplit xmlns="http://tempuri.org/priceSplit.xsd">
  <split>
    <type>Credit Processing Fee</type>
    <acct_code>901010300</acct_code>
    <value>0.30</value>
  </split>
  <split>
    <type>Shipping Fee</type>
    <acct_code>901010500</acct_code>
    <value>0.30</value>
  </split>
  <split>
    <type>Proceeds</type>
    <acct_code>900000000</acct_code>
    <value>9.4000</value>
  </split>
</priceSplit>
I need to explode this data into its' standard tabular design to combine with some additional results, and have been complete unsuccessful. How can I do this?
ASKED: September 25, 2008  8:03 PM
UPDATED: September 25, 2008  8:45 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

This is done using the OPENXML command.

<pre>DECLARE @XML as XML
DECLARE @hDoc as INT
SET @XML = (SELECT Field FROM YourTable WHERE ID = ?)

exec sp_xml_prepairedocument @hDoc OUTPUT, @XML

SELECT *
FROM OPENXML (@hDoc, ‘//’)
WITH (type nvarchar(100) ‘./type/@text’,
acct_code INT ‘./acct_code/@text’,
value MONEY ‘./value/@text’)

exec sp_xml_removedocument @hDoc OUTPUT</pre>

(This isn’t tested but should get you most of the way there.)

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following