I have a stored procedure which works fine when I remove Select 1/0 (this is to test if stored procedure catches error) but when I have Select 1/0 in the stored procedure, it throws an error
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1
I tried the apply solution from TSQL Try / Catch within Transaction or vice versa? but still I still get the same error.
ALTER PROCEDURE [dbo].[PTA_Processing_D365]
AS
BEGIN TRY
BEGIN TRANSACTION test1
UPDATE table A
....
......
......;
INSERT INTO table A(.....)
SELECT .....
FROM table b
WHERE .....;
SELECT 1/0
ROLLBACK TRANSACTION test1
END TRY
BEGIN CATCH
INSERT INTO tableA_ErrorLog ([ExecutedDate], [ERROR_NUMBER], [ERROR_SEVERITY],
[ERROR_STATE], [ERROR_PROCEDURE], [ERROR_LINE], [ERROR_MESSAGE])
SELECT
GETDATE(), ERROR_NUMBER(), ERROR_SEVERITY(),
ERROR_STATE(), ERROR_PROCEDURE(), ERROR_LINE(), ERROR_MESSAGE()
END CATCH