SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Annoying problem with XQuery


Annoying problem with XQuery

Author
Message
zoranrdc
zoranrdc
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: 41
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
LutzM
LutzM
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22785 Visits: 13559
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
zoranrdc
zoranrdc
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: 41
Thanks for your post Lutz.

It solved my problem.

Bye.:-)
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