SQL Clone
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
Graham O'Daniel
Graham O'Daniel
SSC Veteran
SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)SSC Veteran (221 reputation)

Group: General Forum Members
Points: 221 Visits: 58
Mike, this should do the trick. OPENXML method first followed by the .nodes() method.


declare @xml xml
set @xml = '<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>'

declare @Products table ( ProductID int, ProductName varchar(50) )
declare @BOM table ( ParentProductID int, ComponentProductID int, Amount int )

declare @idoc int

exec sp_xml_preparedocument @idoc output, @xml

insert into @Products ( ProductID, ProductName )
select distinct
ID
, Name
from OPENXML ( @idoc, '//Product', 2 )
with (
ID int '@ID'
, Name varchar(50) '@Name'
)

insert into @BOM ( ParentProductID, ComponentProductID, Amount )
select
ParentProductID
, ComponentProductID
, Amount
from OPENXML ( @idoc, '//Product', 2 )
with (
ParentProductID int '../@ID'
, ComponentProductID int '@ID'
, Amount int '@Amount'
)

exec sp_xml_removedocument @idoc

select * from @Products
select * from @BOM

delete @Products
delete @BOM

insert into @Products ( ProductID, ProductName )
select distinct
node.value ( '(@ID)[1]', 'int' )
, node.value ( '(@Name)[1]', 'varchar(50)' )
from @xml.nodes ( '//Product' ) p(node)

insert into @BOM ( ParentProductID, ComponentProductID, Amount )
select
node.value ( '(../@ID)[1]', 'int' )
, node.value ( '(@ID)[1]', 'int' )
, node.value ( '(@Amount)[1]', 'int' )
from @xml.nodes ( '//Product' ) p(node)

select * from @Products
select * from @BOM


paul.morgan-1069605
paul.morgan-1069605
SSC Veteran
SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)SSC Veteran (244 reputation)

Group: General Forum Members
Points: 244 Visits: 87
Thanks for all of this it was exactly what I was trying to solve. I'm using the XQUERY and have all good results except one particular node format.

The node looks like:
<ProductID ProductNo="100086861"></ProductID>

I'd be happy to get the full string but really only need that number.

Using the XQUERY approach can that be done?

Thanks for your time.

P
Mauve
Mauve
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4900 Visits: 2071
paul.morgan-1069605 (11/2/2011)
Thanks for all of this it was exactly what I was trying to solve. I'm using the XQUERY and have all good results except one particular node format.

The node looks like:
<ProductID ProductNo="100086861"></ProductID>

I'd be happy to get the full string but really only need that number.

Using the XQUERY approach can that be done?

Thanks for your time.

P

(Using SQL Server 2008 R2)
DECLARE @xml xml = '<ProductID ProductNo="100086861"></ProductID>';
SELECT @xml.value('/ProductID[1]/@ProductNo', 'int');




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