TRY/CATCH Blocks

  • I am having an issue where my templates seem to have variances. Do these both work or should I pick one over the other?

    Option 1

    SET NOCOUNT ON

    DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int

    BEGIN TRY

    BEGIN TRANSACTION

    --Insert/Update

    COMMIT TRANSACTION

    END TRY

    BEGIN CATCH

    IF @@TRANCOUNT > 0

    ROLLBACK TRANSACTION

    SELECT @ErrMsg = ERROR_MESSAGE(),

    @ErrSeverity = ERROR_SEVERITY()

    RAISERROR(@ErrMsg, @ErrSeverity, 1)

    END CATCH

    Option 2

    SET NOCOUNT ON

    DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int

    BEGIN TRANSACTION

    BEGIN TRY

    --Insert/Update

    END TRY

    BEGIN CATCH

    IF @@TRANCOUNT > 0

    ROLLBACK TRANSACTION

    SELECT @ErrMsg = ERROR_MESSAGE(),

    @ErrSeverity = ERROR_SEVERITY()

    RAISERROR(@ErrMsg, @ErrSeverity, 1)

    END CATCH

    COMMIT TRANSACTION

  • I think I found the answer here:

    http://www.sqlservercentral.com/Forums/Topic1044870-392-1.aspx

    BEGIN TRY

    BEGIN TRANSACTION;

    -- Some code

    COMMIT TRANSACTION;

    END TRY

    BEGIN CATCH

    IF @@TRANCOUNT > 0

    ROLLBACK TRANSACTION;

    END CATCH;

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply