问题描述
我有2个存储过程,用于插入到不同的表。如果我的第一个表插入成功,第二个表失败了如何在第一个表插入之前回滚到初始数据???我已经尝试了几次。在存储过程结束后,mysql连接将自动关闭。那么解决这个问题的另一种方法是什么?我不希望在1个存储过程中加入2个表。
我尝试过:
目前我正在使用启动事务并设置autocommit = 0然后设置一个保存点来回滚语句但它失败。对于这个语句我发现这只适用于一个存储过程。
I got 2 stored procedure that using for insert to different table. if my 1st table insert successful and 2nd table failed how i rollback to initial data before the 1st table insert??? i have already try several times.The mysql connection will auto close after end of stored procedure. So is that another ways to solve this problem?? I dont want join 2 table in 1 stored procedure.
What I have tried:
Currently i m using start transaction and set autocommit=0 then set a savepoint to rollback the statement but its failed.For this statement i found that this is just suitable for one stored procedure only.
推荐答案
BEGIN
START TRANSACTION;
CALL StoredProcedure1(@1, @2)
CALL StoredProcedure2(@1, @2, etc )
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
EXIT PROCEDURE;
END;
COMMIT;
END
这篇关于如何在mysql中回滚数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!