mysql transaction - roll back on any exception -
is possible roll automatically if error occurs on list of mysql commands?
for example along lines of:
begin transaction; insert mytable values1 ... insert mytable values2 ...; -- throw error commit;
now, on execute want whole transaction fail, , therefore should not see values1 in mytable. unfortunately table being pupulated values1 though transaction has errors.
any ideas how make roll back? (again, on error)?
edit - changed ddl standard sql
you can use 13.6.7.2. declare ... handler syntax in following way:
delimiter $$ create procedure `sp_fail`() begin declare `_rollback` bool default 0; declare continue handler sqlexception set `_rollback` = 1; start transaction; insert `tablea` (`date`) values (now()); insert `tableb` (`date`) values (now()); insert `tablec` (`date`) values (now()); -- fail if `_rollback` rollback; else commit; end if; end$$ delimiter ;
for complete example, check following sql fiddle.
Comments
Post a Comment