title: SQL Server 2008: BEGIN TRY and BEGIN TRANSACTION in CURSOR
body: <p>I am trying to loop through a number of records and process each of them. The cursor should continue looping through the records even if an ERROR occurs in processing, but at the end of the batch, I would like to see the error messages of ALL records which produced errors.</p>
  
  <p>I have the following code:</p>
  
  <p>FOR SELECT....</p>
  
  <pre><code>    OPEN cur
      FETCH next FROM cur INTO
      @some_variables
      WHILE @@fetch_status = 0
      BEGIN
  
      BEGIN TRY
        BEGIN TRANSACTION
          ...process the record...
        COMMIT
      END TRY
  
     BEGIN CATCH
     ROLLBACK
  
       RAISERROR ('%s',16, 1, @variable_containing_error)
  
      END CATCH
      END
  
              FETCH next FROM cur INTO
             @some_variables
  
  
      CLOSE cur
      DEALLOCATE cur
  </code></pre>
  
  <p>If I process two records, both with errors, I only get the first error message and not the second records error message.</p>
  
  <p>I am not sure if I am using the BEGIN TRY and TRANSACTION commands properly.</p>
  
  <p>Can someone please help?</p>