SQL事务提交和错误句柄

时间:2016-06-23 19:24:13

标签: sql-server stored-procedures transactions

我的代码如下:

create my sp 
as 
    declare cursor 
    open cursor

    fetch next from 
    begin try
       begin transaction tran1
         exec sp_1.... 
       commit transaction tran1

       select * from table

       begin transaction tran2
           exec sp_2.... 
       commit transaction tran2
    end

    fetch next from cursor
end try

begin catch

SELECT ERROR_MESSAGE() AS ErrorMessage;
IF @@TRANCOUNT > 0
ROLLBACK TRAN;

fetch next from cursor
end catch
close cursor
deallocate cursor

我需要这段代码:

  1. 包含两个事务,并在发生错误时回滚
  2. 当存储过程被杀死时,事务仍将回滚
  3. 我认为catch部分可能不对,因为有两个事务需要具体。

0 个答案:

没有答案