I'd like to know how to update an XML attribute based on the value of another attribute?
In the following example, I'd like to set b="321" where a="banana":
Declare @t table ( x xml )
insert into @t(x) values ('<n1><n2 a="apple" b="2" /><n2 a="banana" b="21" /></n1>')
This select works well to extract the desired value, but how can it be modified?
select m.n.value('@b', 'int') from @t
outer apply x.nodes('/n1/n2') as m(n)
where m.n.value('@a', 'nvarchar(max)') = 'banana'
This update does nothing...
update @t
set x.modify('replace value of (/n1/n2/@b)[1] with "321"')
where x.value('(n1/n2/@b)[1]', 'nvarchar(max)') = 'banana'
select * from @t
This update changes the first node, which isn't the desired result
update @t
set x.modify
(
'replace value of (/n1/n2/@b)[1] with
(
if ( (/n1/n2/@a)[1] = "banana" ) then "321"
else "?help"
)'
)
select * from @t
Thanks in advance for any help...