假设我有一个批处理命令,例如:
BEGIN TRANSACTION
DECLARE @A1 int
SET @A1 = 10
EXEC sp_dosomethings @A1
IF (@@TRANCOUNT > 0) COMMIT TRANSACTION
在存储过程“ sp_dosomethings”中,我想在某些情况下回滚转换:
IF (@A1 = 10) ROLLBACK
现在,当我运行此批处理并将10放置在@ A1中时,事务将回滚,但会引发错误,整个批处理将不起作用!
ERROR: Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0.
那么如何在不导致SQL引发任何错误的情况下从存储过程中回滚事务呢?!