Can anyone help me with the correct TSQL to parse the following xml. Say i wanted to find the item with a value of “y” for the name element, but i want to get the value of of “value” element, which is “2” in the example below.
Declare @XML xml
set @XML ='
<Test>
<items>
<item>
<name>x</name>
<value>1</value>
</item>
<item>
<name>y</name>
<value>2</value>
</item>
</items>
</Test>'
--i am stuck here
selecct @XML.value('Test/items/....")
Result would be “2” by searching for “y”.
Is this possible?
can someone help with the syntax? thanks!
Try this
Breakdown:
The value function cannot be given any expression which can even in theory return more than one value. If in doubt put brackets around it like this
(some-expression-here)[1].To find the value corresponding to a given name: