I followed a recommended template for error handling in a transaction that should work when it's executed inside an existing transaction.
This is my template
CREATE PROCEDURE DoSomething
AS
BEGIN
SET NOCOUNT ON
DECLARE @trans INTEGER = @@TRANCOUNT
IF (@trans > 0)
SAVE TRANSACTION SavePoint
ELSE
BEGIN TRANSACTION
BEGIN TRY
-- code with a check that does a THROW if the requirements aren't met
IF (@trans = 0)
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF (@trans > 0)
ROLLBACK TRANSACTION SavePoint
ELSE
ROLLBACK TRANSACTION
;THROW
END CATCH
END
If I replace the THROW within the TRY block with a RAISERROR, the issue remains.
Test results:
EXEC fail scenario within transaction: Correct result (gives the right error message)
EXEC success scenario within transaction: Gives unexpected error.
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 2.
EXEC fail scenario outside transaction: Gives expected error.
EXEC success scenario outside transaction: Gives unexpected error. The error is the same as above, but every time you execute it, it increments by -1. Does this mean each time more stuff stays uncommitted?
This is how a test looks like:
BEGIN TRANSACTION
EXEC ...
ROLLBACK TRANSACTION
Does anyone know what's going wrong?
Uncomment var
And execute