May 3, 2017 at 8:10 am
I created a store procedure to applied transaction and roll back.
But, roll back never work as expected.
How to fix it?
BEGIN TRAN
--- some codes to insert data into multiple tables
SET @myerror = @@ERROR
IF @myerror <> 0
BEGIN
GOTO ERROR_HANDLER
END
ERROR_HANDLER:
IF @myerror <> 0
BEGIN
ROLLBACK TRAN // never work
END
May 3, 2017 at 8:21 am
adonetek don't know if you can change the code but this is exactly why they brought in the try\catch block to allow this kind of operation. Looking at your code I would say that the call to GOTO ERROR_HANDLER has put the rollback outside of the current transaction so there is nothing there to rollback.
May 3, 2017 at 8:21 am
BEGIN TRY
BEGIN TRANSACTION;
--SQL Goes Here
COMMIT TRANSACTION;Regards,
Matt
May 3, 2017 at 8:44 am
Thank you Matt.
One more question.
I want this store procedure to return a value to application.
I added a parameter
@final int output
If there is no error:
set @final=0
if there an error
set @final=1
How to add @final in try and catch block?
May 3, 2017 at 9:37 am
I would at it after the Commit and Rollback statements, respectively.
Regards,
Matt
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply