如何在mysql中回滚失败的查询?

编程入门 行业动态 更新时间:2024-10-28 21:27:37
本文介绍了如何在mysql中回滚失败的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图捕获在2个不同的表中插入2条记录时发生的错误。如果第二个查询失败,我想回滚已经存储的查询。 我尝试过: 我已经搜索了解决方案,我决定在程序中使用if编码来使用

I am trying to catch the error occurred while inserting 2 records in 2 different tables. I want to rollback the already stored queries if the second query failed. What I have tried: I have searched for the solution , and i decided to use an if codition inside a procedure to check the number of errors using

@@error_count

检查错误的数量,这是我的代码:

, here is my code:

DELIMITER $$ -- This procedure is used to enter the sql commands DROP PROCEDURE IF EXISTS `procedure` $$ CREATE PROCEDURE `procedure`() BEGIN INSERT INTO `mschema`.`table1` (`maxbudget`, `blocked`, `d_percentage`, `max discount`) VALUES ('2250', '0', '.9', '.99'); set @x = @@error_count; if @x= 0 then INSERT INTO `mschema`.`table2` (`name`,`image`,`date`,`fKey_id`) values ('jhon','jfdd', '2018-01-01 00:00:00', LAST_INSERT_ID()); if @x= 0 then commit; else rollback; end if; else rollback; end if; END $$

请注意

Note that

firstdate

表2中的值不重复。 当我执行它时,它成功执行并在MySQL工作台中给出结果如下: 1. - 此程序是用于输入sql命令DROP PROCEDURE如果EXISTS`过程` 2.第二个结果给了我第二次插入查询到表2我试图执行。 所有reult都有

value is not repetitive in table 2. when i execute this, it executed successfully and gives me the result in MySQL workbench as follow: 1. -- This procedure is used to enter the sql commands DROP PROCEDURE IF EXISTS `procedure` 2. The second result gave me the second insertion query into table 2 i am trying to execute. all reult has

0 row(s) affected.

说明插入的值也很重要2个表格不重复。所以我不知道出了什么问题。

It is also important to say that the inserted values in the 2 tables are not repeated. So i do not know what is wrong.

推荐答案

- 此程序用于输入sql命令 DROP PROCEDURE IF EXISTS` procedure` -- This procedure is used to enter the sql commands DROP PROCEDURE IF EXISTS `procedure`

CREATE PROCEDURE`procedation`() BEGIN INSERT INTO`mschema``table1` (`maxbudget`,`closed`,`d_percentage`,`max discount`) VALUES ('2250','0','。 9','.99'); 设置@x = @@ error_count; if @x = 0 then INSERT INTO`mschema``table2` (`name`,`image`,`date`,`fKey_id`) values ('jhon','jfdd','2018-01-01 00:00:00',LAST_INSERT_ID()); 如果@ x = 0则 commit; else rollback; 结束如果; else rollback; 结束如果; END CREATE PROCEDURE `procedure`() BEGIN INSERT INTO `mschema`.`table1` (`maxbudget`, `blocked`, `d_percentage`, `max discount`) VALUES ('2250', '0', '.9', '.99'); set @x = @@error_count; if @x= 0 then INSERT INTO `mschema`.`table2` (`name`,`image`,`date`,`fKey_id`) values ('jhon','jfdd', '2018-01-01 00:00:00', LAST_INSERT_ID()); if @x= 0 then commit; else rollback; end if; else rollback; end if; END

注意

Note that

firstdate

值在表2中没有重复。 当我执行它时,它成功执行并在MySQL工作台中给我结果如下: 1. - 此过程用于输入sql命令DROP PROCEDURE IF EXISTS`procedation` 2.第二个结果给了我第二个插入查询表2我正在尝试执行。 所有reult都有

value is not repetitive in table 2. when i execute this, it executed successfully and gives me the result in MySQL workbench as follow: 1. -- This procedure is used to enter the sql commands DROP PROCEDURE IF EXISTS `procedure` 2. The second result gave me the second insertion query into table 2 i am trying to execute. all reult has

0 row(s) affected.

它也是重要的是,不重复2个表中的插入值。所以我不知道出了什么问题。

It is also important to say that the inserted values in the 2 tables are not repeated. So i do not know what is wrong.

更多推荐

如何在mysql中回滚失败的查询?

本文发布于:2023-10-19 19:34:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508554.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   mysql   中回滚

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!