title: Conditional update XML with T-SQL based on attribute values
body: <p>I have the following:</p>
  
  <pre><code>declare @xml XML
  
  SET @xml = '&lt;record priref="2" creation="2009-12-14T10:39:04"&gt;
        &lt;field tag="aa" occ="1" lang="nl-NL"&gt;somedata&lt;/field&gt;
        &lt;field tag="bb" occ="1" lang="en-US"&gt;somedata&lt;/field&gt;
        &lt;field tag="bb" occ="2" lang="en-US"&gt;somedata&lt;/field&gt;
        &lt;field tag="cc" occ="1"&gt;testdata&lt;/field&gt;
        &lt;field tag="dd" occ="1"&gt;testdata&lt;/field&gt;
        &lt;field tag="ee" occ="1" lang="nl-NL"&gt;somedata&lt;/field&gt;
        &lt;field tag="ee" occ="1" lang="en-US"&gt;somedata&lt;/field&gt;
      &lt;/record&gt;' 
  
  DECLARE @nodeCount int
  DECLARE @i int
  
  SET @i = 1
  
  SELECT @nodeCount = @xml.value('count(/record/field/@lang)','varchar(5)') 
  
  WHILE (@i &lt;= @nodeCount)
  BEGIN
  Set @xml.modify('replace value of (/record/field/@lang)[.="en-US"][1] with "nl-NL"')
  
  SET @i = @i + 1
  END
  
  SELECT @xml
  </code></pre>
  
  <p>and I would like to update the attribute @lang conditionally. </p>
  
  <p>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.</p>
  
  <p>In other cases it has to change like the above query is already doing: change 'en-US' into 'nl-NL'.</p>
  
  <p>Anyone has an idea how to do this? Thanks in advance!</p>