Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using T-SQL to Transform XML Data to a Relational Format


Using T-SQL to Transform XML Data to a Relational Format

Author
Message
Nasir Mirza
Nasir Mirza
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 27
Comments posted to this topic are about the item Using T-SQL to Transform XML Data to a Relational Format
Graham O'Daniel
Graham O'Daniel
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 58
How about using OPENXML?


DECLARE @idoc int, @myXml xml

SET @myXml = '<Author xmlns:xsi="..." xmlns:xsd="..." version="1">
<Benefits>
<Benefit Name="HospitalizationLimit" Value="500000"/>
<Benefit Name="MedicalConsultationLimit" Value="12000"/>
<Benefit Name="LifeInsuranceCoverage" Value="1000000"/>
<Benefit Name="MonthlyTelephone" Value ="5000"/>
<Benefit Name="DependentMemberCount" Value="5"/>
...
</Benefits>
</Author>'

EXEC sp_xml_preparedocument @idoc OUTPUT, @myXml

SELECT *
FROM OPENXML ( @idoc, '/Author/Benefits', 2 )
WITH (
HospitilizationLimit int './Benefit[@Name="HospitalizationLimit"]/@Value',
MedicalConsultationLimit int './Benefit[@Name="MedicalConsultationLimit"]/@Value'
)

EXEC sp_xml_removedocument @idoc




I find OPENXML to be fairly useful when translating XML to table data.

Hope this helps!

-Graham
gary.strange-sqlconsumer
gary.strange-sqlconsumer
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 610
I'm glad you brought up OPENXML Graham. In the past I've used both methods (XQUERY, OPENXML) to process XML data.

MSDN suggests using OPENXML statements with XML data columns. But I can't see how this is possible and how it could be efficient.

If I have an XML column with XML indexes created on it would I want to pass column values to a sp_xml_preparedocument stored procedure and then use OPENXML statements?
Graham O'Daniel
Graham O'Daniel
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 58
Gary,

I would think XML indexes are only useful across multiple rows, like when you want to find rows that contain certain data in the XML or when the XML column would appear in the WHERE clause of a query. If you are processing row-by-row, OPENXML would still be a good option.

Anyone have something else to add?

-Graham
Mauve
Mauve
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1580 Visits: 2053
Graham O'Daniel (12/30/2009)
How about using OPENXML?

I find OPENXML to be fairly useful when translating XML to table data.

Hope this helps!

-Graham

NO! See: Stop Using OPENXML
http://www.sqlservercentral.com/blogs/michael_coles/archive/2008/01/20/stop-using-openxml-please.aspx


(PHB) I think we should build an SQL database. (Dilbert) What color do you want that database? (PHB) I think mauve has the most RAM.
gary.strange-sqlconsumer
gary.strange-sqlconsumer
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 610
Thanks for your post Mauve, it completely clears up what I suspected.

I've created a data feed that archives thoasands of XML files from a web-service, stores them in an indexed xml column and then shreds the data into complex relational hierarchies. The whole thing runs very quickly as a service on the production server. I think using the OPENXML technique will drain the resources and make other services on the server suffer.
Mike-1074278
Mike-1074278
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 29
Any idea on how to do the same for a nested xml structure (e.g. a bill-of-material).
Graham O'Daniel
Graham O'Daniel
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 58
Using OPENXML vs. .nodes is a trade off situation, each with good use cases.

You have to start by asking yourself what you want: performance or resource constraints. If you want performance and don't care about resources then OPENXML is your choice. If you are concerned about resource usage on the server then .nodes is your choice.

I wouldn't suggest one way over the other for all situations.

Please see:
http://sql-server-performance.com/Community/forums/p/25212/147146.aspx
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=93759
http://www.windows-tech.info/15/0a9fd8b0e4b36ccc.php
Graham O'Daniel
Graham O'Daniel
SSC Rookie
SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)SSC Rookie (37 reputation)

Group: General Forum Members
Points: 37 Visits: 58
@Mike: provide me an example of your XML and what you want, and I can write it up for you in both OPENXML and .nodes methods.

-Graham
Mike-1074278
Mike-1074278
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 29
Below an example of the source xml I mean.


<Products>
<Product ID="123" Name="Male Bicycle" Amount="1">
<Product ID="988" Name="Male Frame" Amount="1"/>
<Product ID="501" Name="Wheel" Amount="2">
<Product ID="450" Name="Spoke" Amount="50"/>
<Product ID="490" Name="Rim" Amount="1"/>
</Product>
...
</Product>
<Product ID="234" Name="Female Bicycle" Amount="1">
<Product ID="998" Name="Female Frame" Amount="1"/>
<Product ID="501" Name="Wheel" Amount="2">
<Product ID="450" Name="Spoke" Amount="50"/>
<Product ID="490" Name="Rim" Amount="1"/>
</Product>
...
</Product>
</Products>



I would like to translate to two tables:

Table 1 = Products:
- Product ID
- Product Name
This table will only contain the distinct records from the xml. So product "Rim" will only appear once even though it has been defined for 20 bicycles.

Table 2 = BOM:
- Parent Product ID
- Component Product ID
- Amount
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search