我使用了Save Transaction和rollback,但仍然会发生同样的异常。
它给了我这个错误:
Msg 3903,Level 16,State 1,Procedure HALAQATI_ttttttttttttt,Line 144 ROLLBACK TRANSACTION请求没有对应的BEGIN TRANSACTION。
更新
Msg 50000,Level 16,State 1,Procedure HALAQATI_ttttttttttttt,Line 160 usp_my_procedure_name:102:' ='附近的语法不正确。
存储过程是:
ModelName.ransack(params[:q]).result
答案 0 :(得分:2)
您无法在CATCH
块中回滚,无法首先查询XACT_STATE()
的状态。您的catch块可能在事务已回滚后执行(例如,想想捕获死锁)。
请参阅Exception handling and nested transactions了解正确的模式:
create procedure [usp_my_procedure_name]
as
begin
set nocount on;
declare @trancount int;
set @trancount = @@trancount;
begin try
if @trancount = 0
begin transaction
else
save transaction usp_my_procedure_name;
-- Do the actual work here
lbexit:
if @trancount = 0
commit;
end try
begin catch
declare @error int, @message varchar(4000), @xstate int;
select @error = ERROR_NUMBER(), @message = ERROR_MESSAGE(), @xstate = XACT_STATE();
if @xstate = -1
rollback;
if @xstate = 1 and @trancount = 0
rollback
if @xstate = 1 and @trancount > 0
rollback transaction usp_my_procedure_name;
raiserror ('usp_my_procedure_name: %d: %s', 16, 1, @error, @message) ;
end catch
end