Is this code BEST practise for a basic Transaction ?

  • Is my error checking excessive or not correct ??

    I need to send a errors to my MS Access client via ADO error reporting.

    Below is my template for transactions, <unknown> is filled in by myself for table I am dealing with.

    Please advise

    **************

    CREATE PROCEDURE dbo.Template

    AS

    DECLARE @intErr INT

    DECLARE @vchErrMessage VARCHAR(250)

    DECLARE @vchStr VARCHAR(250)

    SET NOCOUNT ON

    /*

    Description: Notes

    Errors:Notes for errors testing like data checks, none are shown here in this template

    */

    SET TRANSACTION ISOLATION LEVEL READ COMMITTED

    BEGIN TRAN

    BEGIN

     SAVE TRANSACTION START

     BEGIN

      -- TSQL here

      SET @intErr = @@ERROR

      IF @IntErr <> 0

      BEGIN

       SET @vchErrMessage='ERROR('+CONVERT(VARCHAR(4),@intErr)+'): Posting <unknown> data failed.'

       GOTO MYERROR

      END

     END

     COMMIT TRANSACTION

     SET @intErr = @@ERROR

     IF @IntErr <> 0

     BEGIN

      SET @vchErrMessage='ERROR('+CONVERT(VARCHAR(4),@intErr)+'): <unknown> committing transaction failed.'

      GOTO MYERROR

     END

    END

    SET TRANSACTION ISOLATION LEVEL READ COMMITTED

    SET NOCOUNT OFF

    RETURN 0

    MYERROR:

    BEGIN 

     IF @@TRANCOUNT > 0

     BEGIN

      ROLLBACK TRANSACTION START

     END

     SET NOCOUNT OFF

     SET TRANSACTION ISOLATION LEVEL READ COMMITTED

     RAISERROR (@vchErrMessage,16,1) WITH NOWAIT

     RETURN @intErr

    END

  • Here's an interesting article on error handling.

    http://www.sommarskog.se/error-handling-II.html#presumptions

  • Thats where I learnt it, I was just after a second opinion, to see if I have done good !

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

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