Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a trasanction cover several stored procedure?

I need to include several stored procedure in a single transaction in a single database, if any of stored procedure fail then roll back transaction of all stored procedure procesed in the scope.

I work with SQL-SERVER 2008

like image 332
RicardoBalda Avatar asked Oct 23 '25 18:10

RicardoBalda


2 Answers

begin transaction
begin try
  exec proc_1
  exec proc_2
  exec proc_3
  commit transaction
end try
begin catch
  rollback transaction
end catch
like image 69
Ray Avatar answered Oct 26 '25 10:10

Ray


You can create a single stored procedure that starts a transaction and then calls the other stored procedures. If any of the inner stored procedures fail you can rollback the transaction. If you tell us what database platform you're using (MS SQL Server, MySQL, etc.) people may be able to provide more specific solutions.

like image 22
TLiebe Avatar answered Oct 26 '25 09:10

TLiebe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!