SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XACT_ABORT with recursive stored proc


XACT_ABORT with recursive stored proc

Author
Message
thbaig
thbaig
Say Hey Kid
Say Hey Kid (661 reputation)Say Hey Kid (661 reputation)Say Hey Kid (661 reputation)Say Hey Kid (661 reputation)Say Hey Kid (661 reputation)Say Hey Kid (661 reputation)Say Hey Kid (661 reputation)Say Hey Kid (661 reputation)

Group: General Forum Members
Points: 661 Visits: 496
Hi,
we use XACT_ABORT to rolback if statements failed.

I have a scenario where I have a stored proc that call other stored proc inherently.

Store Proc Main hold following

Begin
Exec StoredProc1; -- this stored proc perform insert
Exec StoredProc2; -- this stored proc perform insert.Update
End;

Now can I use XACT_ABORT in main stored proc, so if any of sp failed (StoredProc1,StoredProc2....), it may rollback all ?

Begin
xact_abort on
Exec StoredProc1; -- this stored proc perform insert
Exec StoredProc2; -- this stored proc perform insert.Update
xact_abort off
End;
Orlando Colamatteo
Orlando Colamatteo
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42356 Visits: 14413
XACT_ABORT may work, but I would recommend you use TRY/CATCH instead for managing exceptions and transactions.


BEGIN TRY;

BEGIN TRAN;

Exec StoredProc1; -- this stored proc perform insert
Exec StoredProc2; -- this stored proc perform insert.Update

COMMIT;

END TRY
BEGIN CATCH
IF XACT_STATE() != 0
ROLLBACK TRAN;

DECLARE @err_str VARCHAR(2048),
@err_sev INT,
@err_state INT;

SELECT @err_str = ERROR_MESSAGE(),
@err_sev = ERROR_SEVERITY(),
@err_state = ERROR_STATE();

RAISERROR(@err_str, @err_sev, @err_state);
END CATCH;



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search