yep - it's likely you don't need a cursor. But in regards to this code you probably may need to handle the close and dealocate cursor in the catch block. Your test code also indicates that you have an expicit transaction but you didn't include the code - that can affect how you code your error handling. Here is one way to continue on looping even if an error occurs for one of the iterations:--don't let an error rollback your explicit transactionset xact_abort off set nocount oncreate table #t (database_id int)declare @dbid intcontinueCursor:select @dbid = isNull(@dbid, -1)begin try begin tran declare crs cursor for select database_id from sys.databases where database_id > @dbid open crs fetch next from crs into @dbid while @@fetch_status = 0 begin --Do some work insert #t values (@dbid) --Raise a test error for one of the values if @dbid = 4 raiserror('test error', 17, 1) fetch next from crs into @dbid end close crs deallocate crs commit tranend trybegin catch select error_message() [Error] if cursor_status('global', 'crs') > -1 close crs if cursor_status('global', 'crs') = -1 deallocate crs goto continueCursorend catchselect * from #tdrop table #t
Be One with the OptimizerTG