|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 4:15 AM
Points: 3,
Visits: 34
|
|
Hi,
I have following xml document:
<Root> <Header> <SomeTag1></SomeTag1> <SomeTag2></SomeTag2> </Header> <Data> <Item ID="1" AdditionalInfo="Some info"> <SubItem ID="1">1</SubItem> <SubItem ID="2">2</SubItem> <SubItem ID="3">3</SubItem> </Item> <Item ID="2" AdditionalInfo="Some info"> <SubItem ID="1">1</SubItem> <SubItem ID="2">2</SubItem> </Item> </Data> </Root>
I need to extra each SubItem from the xml document into the following table:
Data ( SubItemId sometype, SubItemValue sometype, ItemId sometype, AdditionalInfo somety),
where 'ItemId' and 'AdditionalInfo' are parent's values and 'SubItemValue' is text value of 'SubItem' tag.
How to solve this problem in the most efficient way ?
Thanks in advance,
Zoran
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 3:17 PM
Points: 6,731,
Visits: 12,131
|
|
Something like this (assuming you're using SS2K5 or above)?
DECLARE @xml XML SELECT @xml='<Root> <Data> <Item ID="1" AdditionalInfo="Some info"> <SubItem ID="1">1</SubItem> <SubItem ID="2">2</SubItem> <SubItem ID="3">3</SubItem> </Item> <Item ID="2" AdditionalInfo="Some info"> <SubItem ID="1">1</SubItem> <SubItem ID="2">2</SubItem> </Item> </Data> </Root>'
SELECT y.value('@ID[1]','INT') AS SubItemId, y.value('.','INT') AS SubItemValue, c.value('@ID[1]','INT') AS ItemId, c.value('@AdditionalInfo[1]','varchar(30)') AS AdditionalInfo FROM @xml.nodes('Root/Data/Item') T(c) CROSS APPLY c.nodes('SubItem') X(y)
Lutz A pessimist is an optimist with experience.
How to get fast answers to your question How to post performance related questions Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 4:15 AM
Points: 3,
Visits: 34
|
|
Thanks for your post Lutz.
It solved my problem.
Bye.
|
|
|
|