Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Script with rollback on error

I am trying to create a transaction in MySql which will roll back when an exception occurs during the transaction. Similar using the following in a stored procedure.

DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK; 

Can this be done without writing a stored procedure? For example the following snippet should roll back the first insert because the second insert would fail.

START TRANSACTION;
INSERT INTO mytable VALUE 'foo1',2,'foo3','foo4'
INSERT INTO mytable VALUE 'foo1','foo2','foo3','foo4'
COMMIT
like image 912
Damien Avatar asked Oct 09 '12 14:10

Damien


1 Answers

If I understand correctly you want to run a SQL script containing, for example inserts, in a single transaction that will rollback if any of the inserts fail. Is right?

I'm not 100% on this, but I'm pretty sure you'd need to do this as a stored procedure or programatically in, say, Java.

like image 146
ct_ Avatar answered Sep 28 '22 04:09

ct_