title: Dynamically replacing the value of a node in XML DML
body: <p>I am struggling with this now: How do you replace the value of a node in an untyped XML column where the text is equal to a certain variable value? Is it possible?</p>
  
  <p>My XML:</p>
  
  <pre><code>&lt;attrs&gt;
    &lt;attr&gt;ManualInsert&lt;/attr&gt;
    &lt;attr&gt;ManualInsert2&lt;/attr&gt;
    &lt;attr&gt;ManualInsert4&lt;/attr&gt;
    &lt;attr&gt;ManualInsert8&lt;/attr&gt;
  &lt;/attrs&gt;
  </code></pre>
  
  <p>My Tries:</p>
  
  <pre><code>DECLARE @OldValue Varchar(255) =  'ManualInsert'
  DECLARE @NewValue Varchar(255) =  'ReplacedValue'
  
  UPDATE
      Labels
  SET
      Attributes.modify('replace value of (/attrs/attr/text())[1]
                         with
                         if ((/attrs/attr/text() = sql:variable("@OldValue")))
                         then sql:variable("@NewValue")
                         else () ')
  WHERE
      Id = 2000046
  </code></pre>
  
  <p>message:  <code>(0 row(s) affected)</code></p>
  
  <pre><code>DECLARE @OldValue Varchar(255) =  'ManualInsert'
  DECLARE @NewValue Varchar(255) =  'ReplacedValue'
  
  UPDATE
      Labels
  SET
      Attributes.modify('replace value of (/attrs/attr[text() = sql:variable("@OldValue")])[1]
                         with sql:variable("@NewValue")')
  WHERE
      Id = 2000046
  </code></pre>
  
  <p>message: </p>
  
  <pre><code>Msg 2356, Level 16, State 1, Line 7
  XQuery [Labels.Attributes.modify()]: The target of 'replace value of' must be a non-metadata attribute or an element with simple typed content, found 'element(attr,xdt:untyped) ?'
  </code></pre>
  
  <p>expected result:</p>
  
  <pre><code>&lt;attrs&gt;
    &lt;attr&gt;ReplacedValue&lt;/attr&gt;
    &lt;attr&gt;ManualInsert2&lt;/attr&gt;
    &lt;attr&gt;ManualInsert4&lt;/attr&gt;
    &lt;attr&gt;ManualInsert8&lt;/attr&gt;
  &lt;/attrs&gt;
  </code></pre>