title: Inserting node in XML using SQL
body: <p>I have the below XML:</p>
  
  <pre><code>create table #temp(cid int,xml_data xml)
  insert into cid
  values(1001,
       '&lt;Main&gt;
          &lt;name&gt;''John doe''&lt;/name&gt;
          &lt;age&gt;15&lt;/age&gt;
      &lt;/Main&gt;')
  </code></pre>
  
  <p>I want to add an additional node to this XML based on a simple parametric condition:</p>
  
  <p>desired output:</p>
  
  <pre><code>&lt;Main&gt;
      &lt;name&gt;John doe&lt;/name&gt;
      &lt;type&gt;Q&lt;/type&gt;
      &lt;age&gt;15&lt;/age&gt;
  &lt;/Main&gt;
  </code></pre>
  
  <p>code:</p>
  
  <pre><code>select case when @type = 'Q' then
      UPDATE #temp
      SET Main.modify('insert &lt;type = 'Q'&gt; into 
          (/Main)')
      GO
  </code></pre>
  
  <p>I am getting syntax error. Any help!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I implemented the suggested solution in my code and I'm getting below error. Missing out something silly!</p>
  
  <pre><code> UPDATE #temp
           SET xml_data = 
              case
                  when @type = 'G' 
                  then xml_data.modify('insert &lt;type&gt;G&lt;/type&gt; into (/Main)[1]');
                  when @type = 'Q' 
                  then xml_data.modify('insert &lt;type&gt;Q&lt;/type&gt; into (/Main)[1]'); end
  </code></pre>
  
  <p>I am getting 'Incorrect use of the XML data type method 'modify'. A non-mutator method is expected in this context.' error</p>