Transaction With Throw

Transaction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
USE [DocumentPacks];

BEGIN TRY
BEGIN TRANSACTION

-- DECLARE

-- INSERT

-- SOMETHING WENT WRONG WITH THE CODE ABOVE
THROW 51000, 'Foo error.', 1;

COMMIT TRANSACTION
END TRY
BEGIN CATCH
DECLARE @ErrorNumber INT = ERROR_NUMBER();
DECLARE @ErrorLine INT = ERROR_LINE();
DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
DECLARE @ErrorState INT = ERROR_STATE();

IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;

RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
END CATCH

References