I'm not too familiar with cursors, but I just need to know one relatively simple thing. Take a look at the structure of the script below and note where the cursor is instantiated and where it is closed/deallocated. If the script deadlocks where I've written /* most of the code here */ and the transaction is rolled back, then reattempted, what happens when the script tries to fetch next? Since the execution never reached the close/deallocate cursor lines, I feel as though on the second attempt the the cursor would fetch the second row. Note that I'm not claiming that this is correctly written - I feel as though the issue I have is due to the cursor being deallocated AFTER committing the transaction.
declare LPCursor cursor for
/*
...
*/
while (@deadlockretries <= @Maxlockretries)
begin
begin try
begin transaction
fetch next from LPCursor into @var1, @var2, @var3
while (@@fetch_status = 0)
begin
/* most of the code here */
end
commit transaction
close LPCursor
deallocate LPCursor
end try
begin catch
if (error_number() = 1205)
begin
if xact_state() <> 0
begin
rollback transaction
end
end
end catch
end