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


Reading match pair of descandats of XML elements in xquiry through T-Sql


Reading match pair of descandats of XML elements in xquiry through T-Sql

Author
Message
danyc
danyc
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 236
In the following XML

<Recordset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Products>
<Productdetails>
<Productkey>520000522</Productkey>
</Productdetails>
<Productdetails>
<Productkey>816700333</Productkey>
</Productdetails>
<Productdetails>
<Productkey>520444422</Productkey>
</Productdetails>

<Productcategory>
<Productcat>abc</Productcat>
</Productcategory>
</Products>
<Products>
<Productdetails>
<Productkey>53330511</Productkey>
</Productdetails>
<Productcategory>
<Productcat>def</Productcat>
</Productcategory>
</Prtoducts>



<Products>
<Productdetails>
<Productkey>511144122</Productkey>
</Productdetails>
<Productdetails>
<Productkey>511765122</Productkey>
</Productdetails>
<Productcategory>
<Productcat>fgh</Productcat>
</Productcategory>
</Prtoducts>
<Products>
<Productdetails>
<Productkey>58800512</Productkey>
</Productdetails>
<Productcategory>
<Productcat>ter</Productcat>
</Productcategory>
</Products>
</Recordset>

, I wrote the following statement to get Productkey of Productdetails in sequence of the <Products>.
declare @XML xml
set @XML = -- the above XML

SELECT
doc.col.value('Productkey[1]', 'int')
FROM @xml.nodes(' Recordset/Products/Productdetails/Productkey/')
doc(col)


SELECT
doc.col.value('Productcat[1]', 'nvarchar(10)')
FROM @xml.nodes(' Recordset/Products/Productcategory/Productcat/')
doc(col)

I got the <productkey> and the <productcat> separately in different column tables.
But how I can a match pair of the proper <productkey> and <productcat> in two col in the same table or how I can read matched pair col at once ?

For example pair values (520000522,'abc'), (816700333,'abc'),(520444422,'abc'),(53330511,'def') etc

Thank in advanced
For reply
Eugene Elutin
Eugene Elutin
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5118 Visits: 5478

declare @XML xml
set @XML =
'<Recordset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Products>
<Productdetails>
<Productkey>520000522</Productkey>
</Productdetails>
<Productdetails>
<Productkey>816700333</Productkey>
</Productdetails>
<Productdetails>
<Productkey>520444422</Productkey>
</Productdetails>
<Productcategory>
<Productcat>abc</Productcat>
</Productcategory>
</Products>
<Products>
<Productdetails>
<Productkey>53330511</Productkey>
</Productdetails>
<Productcategory>
<Productcat>def</Productcat>
</Productcategory>
</Products>
<Products>
<Productdetails>
<Productkey>511144122</Productkey>
</Productdetails>
<Productdetails>
<Productkey>511765122</Productkey>
</Productdetails>
<Productcategory>
<Productcat>fgh</Productcat>
</Productcategory>
</Products>
<Products>
<Productdetails>
<Productkey>58800512</Productkey>
</Productdetails>
<Productcategory>
<Productcat>ter</Productcat>
</Productcategory>
</Products>
</Recordset>'


SELECT doc.col.value('Productkey[1]', 'int'),doc.col.value('../Productcategory[1]/Productcat[1]', 'varchar(10)')
FROM @xml.nodes('Recordset/Products/Productdetails') doc(col)



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
danyc
danyc
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 236
Thank
Eugene Elutin for editing my XML.
I resolved my problem. it was a matter of decompund of third or fourth xml hirarchy level by nodes.query method and then i got the solution.:-)
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