DECLARE @TranCounter INT
BEGIN TRY
SET @TranCounter = @@TRANCOUNT
IF @TranCounter > 0
BEGIN
SAVE TRANSACTION preSavePoint;
END
ELSE
BEGIN
BEGIN TRANSACTION;
END
.........
IF @TranCounter = 0
BEGIN
COMMIT TRANSACTION
END
END TRY
BEGIN CATCH
IF @TranCounter = 0
BEGIN
ROLLBACK TRANSACTION
END
ELSE IF XACT_STATE() <> -1
BEGIN
ROLLBACK TRANSACTION preSavePoint
DECLARE @ErrorMessage NVARCHAR(4000)
DECLARE @ErrorSeverity INT
DECLARE @ErrorState INT
SELECT @ErrorMessage = ERROR_MESSAGE()
SELECT @ErrorSeverity = ERROR_SEVERITY()
SELECT @ErrorState = ERROR_STATE()
RAISERROR (@ErrorMessage,@ErrorSeverity,@ErrorState)
END
END CATCH