如何在mysql中回滚数据? [英] How to rollback data in mysql?

查看:572
本文介绍了如何在mysql中回滚数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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.

推荐答案

我想知道你是否需要创建一个调用存储过程1然后存储过程2的'主'存储过程......这样你可以做类似
$ b的事情$ b

Im wondering if you need to create a 'master' stored procedure that calls stored procedure 1 and then stored procedure 2 ... that way, you could do something like

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中回滚数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆