sql - Modify XML values identified through cross apply -
i've got data issue values stored in xml column in database. i've reproduced problem following example:
setup script:
create table xmltest ( [xml] xml ) --a row 2 duff entries insert xmltest values (' <root> <item> <flag>false</flag> <frac>0.5</frac> </item> <item> <flag>false</flag> <frac>0</frac> </item> <item> <flag>false</flag> <frac>0.5</frac> </item> <item> <flag>true</flag> <frac>0.5</frac> </item> </root> ')
in xml portion incorrect entries <flag>false</flag>
, <frac>0.5</frac>
value of flag
should true
non-zero frac
values.
the following sql identifies xml item nodes require update:
select i.query('.') xmltest cross apply xml.nodes('root/item[flag="false" , frac > 0]') x(i)
i want update correct these nodes, don't see how modify item
elements identified cross apply
. saw update looking this:
update t set x.i.modify('replace value of (flag/text())[1] "true"') xmltest t cross apply xml.nodes('root/item[flag="false" , frac > 0]') x(i)
however isn't working: error "incorrect syntax near 'modify'".
can done through method?
i know alternative string replace on xml column, don't being bit unsubtle (and i'm not confident wouldn't break things in real-word problem)
it not possible update 1 xml instance in more 1 place @ time have updates in loop until done.
from http://msdn.microsoft.com/en-us/library/ms190675.aspx "expression1: identifies node value updated. must identify single node."
-- while there rows needs updated while exists(select * xmltest [xml].exist('root/item[flag="false" , frac > 0]') = 1) begin -- update first occurence in each xml instance update xmltest set [xml].modify('replace value of (root/item[flag="false" , frac > 0]/flag/text())[1] "true"') xml.exist('root/item[flag="false" , frac > 0]') = 1 end
Comments
Post a Comment