title: How to update top 1 row in SQL Server
body: <p>I want to update latest row in the table. For that I can use </p>
  
  <pre><code>select top 1 ...
  </code></pre>
  
  <p>but how to do this with update?</p>
  
  <pre><code>DECLARE @Result nvarchar(50)
  
  SET @Result = (select top 1 ROUND(SUM(DATEDIFF(SS,CheckIn,CheckOut) / 60.0 / 60.0),2)  from CheckInCheckOut)
  
  update CheckInCheckOut 
  set CountHours= @Result 
  where UserId_Fk = 3
  </code></pre>
  
  <p>I want to do somewhat like this:</p>
  
  <pre><code>update top 1 CheckInCheckOut
  </code></pre>
  
  <p>Example would be appreciated.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I got the correct query as :</p>
  
  <pre><code>  DECLARE @Result nvarchar(50)
   SET @Result = (select top 1 ROUND(SUM(DATEDIFF(SS,CheckIn,'2014-05-15 03:30:32.507') / 60.0 / 60.0),2)  from CheckInCheckOut where UserId_Fk=3 order by CheckInCheckOutId desc) 
  
  update CheckInCheckOut 
  set CheckOut='2014-05-15 03:30:32.507',CountHours=@Result 
  where CheckInCheckOutId = (select top (1) CheckInCheckOutId  
                 from CheckInCheckOut 
                 where UserId_Fk = 3
                 order by CheckInCheckOutId desc )
  </code></pre>
  
  <p>But I get this error as </p>
  
  <pre><code>    'Column "CheckInCheckOut.CheckInCheckOutId" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
  </code></pre>
  
  <p>'</p>
  
  <p>How can I use order by in my select statement?</p>