multiple store procedure in single transaction batch

  • Dear Sir,

    I want to write a procedure which contains several store procedures in

    single batch transaction. my main purpose is if all sp is successfull then

    commit transaction is executed if any one of them fails then execute rollback transaction.

    but which one will sucessfull and which one will be fails how to keep the track, I can't understand. so please help me with a sample code.

    Thanking You

    Debasis Bag

    Suppose :-

    Create procedure xx

    set nocount on

    begin Transactions

    Begin

    --Inserting data table 1

    sp_InsertData1

    --Inserting Data Table 2

    sp_InsertData2

    End

    If @@error<>0 then goto errorHandler

    commit transaction

    return

    errorHandler:

    rollback transaction

    return @@Error

    Go

    Note 'Here is no way to control track of error in one single transaction batch.

  • You could check for @@error <> 0 after each procedure call and use RAISERROR with a meaningful error message.

    -- Gianluca Sartori

  • You could also use return values to validate success/failure

    declare @retval int

    --Inserting data table 1

    exec @retval = sp_InsertData1

    -- SUCCESS: @retval = 0

    -- FAILURE: @retval = 1

    If @retval = 1 then goto errorHandler

    --Inserting Data Table 2

    exec @retval = sp_InsertData2

    If @retval = 1 then goto errorHandler

    _____________________________________________________________________
    - Nate

    @nate_hughes

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

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