Get Scope_Identity after error check

  • Hi,

    I was wondering if the following code will work after an Insert statement.

    -- Check For Errors

    SELECT @myERROR = @@ERROR, @myRowCount = @@ROWCOUNT

    IF @myERROR != 0 GOTO HANDLE_ERROR

    RETURN 0

    SET @OrderId = SCOPE_IDENTITY()

    Will the SET @OrderId = SCOPE_IDENTITY() still give the id that was created if it comes after the error check?

  • Yes.

    But you can exclude any doubts by doing this in one go:

    SELECT @myERROR = @@ERROR,

    @myRowCount = @@ROWCOUNT,

    @OrderId = SCOPE_IDENTITY()

    IF @myERROR != 0 GOTO HANDLE_ERROR

    RETURN 0

    _____________
    Code for TallyGenerator

  • Cool, thank you for your answer and suggestion it is much appreciated!

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

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