rollback in script

  • Hi everyone, im busy writing a script but my try catch and subsequent rollback function is not working can anyone shed some light?

    ive coded my rollback function within my catch function but even though i have errors in some of my insert and delete statements it never executes the catch block.

    what am i doing wrong here?

    BEGIN TRY

    BEGIN TRANSACTION

    //ALL my INSERT, UPDATE, DELETE statements IN here

    COMMIT;

    END TRY

    BEGIN CATCH

    IF @@TRANCOUNT > 0

    ROLLBACK

    PRINT 'Rollback executed, script contains errors.'

    DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int

    SELECT @ErrMsg = ERROR_MESSAGE(),

    @ErrSeverity = ERROR_SEVERITY()

    RAISERROR(@ErrMsg, @ErrSeverity, 1)

    END CATCH

  • I think you have to put your TRY block inside of your transaction. Just switch the order of the BEGIN TRY and BEGIN TRAN statements and try it.

    Tim

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

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