title: Updating XML attribute in SQL Server XML column
body: <p>I am trying to update a node of in my XML which is stored in a SQL Server XML column, the line below works if my XML is in a XML element but now I somehow need to change it to XML attributes, apparently the line becomes invalid after the change.</p>
  
  <p>Works for XMLElement:</p>
  
  <pre><code>UPDATE  [Customers] 
  SET  voucherXML.modify('replace value of (/ArrayOfCampaignVoucher/CampaignVoucher/Qty/text())[1] with "50"') 
  WHERE  voucherXML.value('(/ArrayOfCampaignVoucher/CampaignVoucher/VouCode)[1]', 'nvarchar(50)') = @VoucherCode 
  </code></pre>
  
  <p>I tried changing the statement like this but no luck, no errors but <code>QTY</code> values doesn't get change to the value of <code>@NewQuantity</code>:</p>
  
  <pre><code>UPDATE [Customers]  
         SET voucherXML='&lt;ArrayOfCampaignVoucher xmlns:xsd="http://www.w3.org/2001/XMLSchema" Qty="' + CAST(@NewQuantity AS NVARCHAR(16)) + '" /&gt;'  
     WHERE voucherXML.value('(/CampaignVoucher/VouCode)[1]', 'nvarchar(50)') = @VoucherCode 
  </code></pre>
  
  <p>This is how my XML looks in the SQL Server XML column:</p>
  
  <pre><code>&lt;ArrayOfCampaignVoucher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;CampaignVoucher VouCode="Vouc001" Qty="16" /&gt;
    &lt;CampaignVoucher VouCode="Vouc002" Qty="18" /&gt;
    &lt;CampaignVoucher xsi:nil="true" /&gt;
  &lt;/ArrayOfCampaignVoucher&gt;
  </code></pre>