title: How to catch error for a transaction in T-SQL for SQL Server?
body: <p>In SQL Server, you need to check error for each SQL statement. For example if I have 3 updates in one transaction, I need some code like:</p>
  
  <pre><code>declare @HasError int
  begin tran
  Update tab1 set ....
  Set  @HasError = @@error;
  Update tab2 set ...
  Set  @HasError = @@error;
  Update tab3 set ...
  Set  @HasError = @@error;
  
  If @HasError&lt;&gt;0
    rollback tran;
  else
   commit tran;
  </code></pre>
  
  <p>Any other solution for this case with more simple code? for instance, something like c# style:</p>
  
  <pre><code>   begin tran
      try
      {
        Update tab1 set ....    
        Update tab2 set ...
        Update tab3 set ...
        commit tran;
      }catch(error){
         rollback tran;
      }
  </code></pre>