I have the following:
declare @xml XML
SET @xml = '<record priref="2" creation="2009-12-14T10:39:04">
<field tag="aa" occ="1" lang="nl-NL">somedata</field>
<field tag="bb" occ="1" lang="en-US">somedata</field>
<field tag="bb" occ="2" lang="en-US">somedata</field>
<field tag="cc" occ="1">testdata</field>
<field tag="dd" occ="1">testdata</field>
<field tag="ee" occ="1" lang="nl-NL">somedata</field>
<field tag="ee" occ="1" lang="en-US">somedata</field>
</record>'
DECLARE @nodeCount int
DECLARE @i int
SET @i = 1
SELECT @nodeCount = @xml.value('count(/record/field/@lang)','varchar(5)')
WHILE (@i <= @nodeCount)
BEGIN
Set @xml.modify('replace value of (/record/field/@lang)[.="en-US"][1] with "nl-NL"')
SET @i = @i + 1
END
SELECT @xml
and I would like to update the attribute @lang conditionally.
If there is a node with the same attribute value combination for ‘tag’ and ‘occ’ (like tag=ee) the attribute value for @lang has to stay unchanged.
In other cases it has to change like the above query is already doing: change ‘en-US’ into ‘nl-NL’.
Anyone has an idea how to do this? Thanks in advance!
This should do it
However, SQL is not very fast with these types of manipulations. Might be more effective to do this in the app code or put some CLR function together.