Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let me change a little your update query:</p> <pre><code> declare @passedXML xml set @passedXML='&lt;root&gt;&lt;record&gt;&lt;name&gt;Sam&lt;/name&gt;&lt;age&gt;37&lt;/age&gt;&lt;comments /&gt;&lt;/record&gt;&lt;record&gt;&lt;name&gt;Dan&lt;/name&gt;&lt;age&gt;32&lt;/age&gt;&lt;comments /&gt;&lt;/record&gt;&lt;/root&gt;' DECLARe @test TABLE (id bigint IDENTITY(1,1),name nvarchar(100), age int, comments varchar(255)) insert into @test (name, age, comments) select x.record.query('name').value('.', 'varchar(255)'), x.record.query('age').value('.', 'int'), x.record.query('comments').value('.','varchar(255)') from @passedXML.nodes('root/record') as x(record) set @passedXML='&lt;root&gt;&lt;record&gt;&lt;id&gt;99999&lt;/id&gt;&lt;comments&gt;This is a new comment&lt;/comments&gt;&lt;/record&gt;&lt;record&gt;&lt;id&gt;2&lt;/id&gt;&lt;comments&gt;Michael Michael&lt;/comments&gt;&lt;/record&gt;&lt;/root&gt;' ; with CTE as( SELECT x.record.query('comments').value('.','varchar(255)') comment, x.record.query('id').value('.','int') id from @passedXML.nodes('root/record') as x(record)) update @test set comments = cte.comment FROM @test T JOIN CTE oN cte.id=t.id </code></pre> <p>Update result: <code>(1 row(s) affected)</code></p> <p>SQL server is smart enough to do not update every row. If you look at execution plan, you can see that hash match has only one actual number of rows, not two:</p> <p><img src="https://i.stack.imgur.com/l4NAr.png" alt="enter image description here"> </p> <p>@@RoWCOUNT will show to you 1 too and it is ok.</p> <p>In your example you update <strong>every</strong> row already (if in XML there are no such id, NULL should be inserted in comments)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload