Friday, March 23, 2012

loop through xml nodes in sql

Hi,

I would like to loop through myXml nodes and modify them in sql.

i have got something like this:

<Root>

<ValueHolder>

<Value>3.00</Value>

<IsNoteDirty>false</IsNoteDirty>

<Timestamp>

<StampType>Month</StampType>

<Stamp>3</Stamp>

<Year>2007</Year>

</Timestamp>

</ValueHolder>

<ValueHolder>

<Value xsi:nil="true" />

<IsNoteDirty>false</IsNoteDirty>

<Timestamp>

<StampType>Month</StampType>

<Stamp>4</Stamp>

<Year>2007</Year>

</Timestamp>

</ValueHolder>

<ValueHolder>

<Value>2.00</Value>

<IsNoteDirty>false</IsNoteDirty>

<Timestamp>

<StampType>Month</StampType>

<Stamp>5</Stamp>

<Year>2007</Year>

</Timestamp>

</ValueHolder>

<ValueHolder>

<Value>5.00</Value>

<IsNoteDirty>false</IsNoteDirty>

<Timestamp>

<StampType>Month</StampType>

<Stamp>6</Stamp>

<Year>2007</Year>

</Timestamp>

</ValueHolder>

</Root>

And i want to loop over all the "Value" nodes and modify their value.

How can I do it?

This should get you started. The following query will list out the value. From there, it will depend on what you want updated before we update it. Hope this helps.
Tim

declare @.xml xml
set @.xml = '
<Root>
<ValueHolder>
<Value>3.00</Value>
<IsNoteDirty>false</IsNoteDirty>
<Timestamp>
<StampType>Month</StampType>
<Stamp>3</Stamp>
<Year>2007</Year>
</Timestamp>
</ValueHolder>
<ValueHolder>
<Value>23.00</Value>
<IsNoteDirty>false</IsNoteDirty>
<Timestamp>
<StampType>Month</StampType>
<Stamp>3</Stamp>
<Year>2007</Year>
</Timestamp>
</ValueHolder>
</Root>'

select Tab.Col.value('(Value)[1]','MONEY')
from
@.xml.nodes('/Root/ValueHolder') Tab(Col)

No comments:

Post a Comment