Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Try/Catch in Cursor It Breaks After Failures

Author  Topic 

GhantaBro
Posting Yak Master

215 Posts

Posted - 2009-08-10 : 08:18:43
Hey GUys,

I do not need the cursor to break after failures and want to iterate through all the records... Any suggestion how can I change this code below? Thanks!

BEGIN TRY
DECLARE test_cursor CURSOR STATIC FOR
SELECT [file_name]
, op_format
FROM ABC..test --- THIS IS FOR TESTING PURPOSE!!!!!!!!!


OPEN test_cursor

FETCH NEXT FROM test_cursor INTO @var1, @var2

WHILE @@FETCH_STATUS = 0
BEGIN

<<<>>>>


Update log_table
set record_count = @rowcnt
where [file_name] = @var1


FETCH NEXT FROM test_cursor INTO @file_name, @delimeter_type

END

CLOSE test_cursor
DEALLOCATE test_cursor

END TRY

BEGIN CATCH

IF XACT_STATE() = -1 ROLLBACK

SET @error_msg = error_message()

UPDATE log_table
SET failure = 'file cannot be processed due to error: ' + @error_msg + '; at line ' + cast(error_line() as varchar)
WHERE [file_name] = @var1

END CATCH

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2009-08-10 : 08:40:32
Yeah, Don't use a cursor.

Other than that, you'll have to test @@error after each statement and resume running if the error isn't to severe.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-10 : 10:59:52
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 transaction
set xact_abort off

set nocount on

create table #t (database_id int)

declare @dbid int

continueCursor:

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 tran

end try
begin catch
select error_message() [Error]

if cursor_status('global', 'crs') > -1
close crs
if cursor_status('global', 'crs') = -1
deallocate crs

goto continueCursor
end catch

select * from #t
drop table #t


Be One with the Optimizer
TG
Go to Top of Page

GhantaBro
Posting Yak Master

215 Posts

Posted - 2009-08-10 : 11:19:42
Thanks a lot guys.. I will test this today.
Go to Top of Page

markkenji0712
Starting Member

1 Post

Posted - 2009-10-29 : 09:17:15
hi, have you tested this already. and how did it go?
Go to Top of Page

sqlpal2007
Posting Yak Master

200 Posts

Posted - 2009-11-06 : 10:38:29
Hi ALL,

I am having the same problem except I have multiple updates in my cursor. I have to rollback even if one update fails and insert the cursor varialbes in temp table and move on to the next record.

How would I achieve this?

Thanks guys,

-S
Go to Top of Page
   

- Advertisement -