Is it possible to somehow determine that after the last INSERT / DELETE / UPDATE operation an error occurred to make a conditional rollback of an operation in a transaction?

For example, in MS SQL Server for this task, you can use the system variable @@ERROR , which takes a nonzero value when an error occurs after the last operation. Here is its usage:

 BEGIN TRANSACTION INSERT INTO Customers(cust_id, cust_name) VALUES('1000000010', 'Toys Emporium'); SAVE TRANSACTION StartOrder; INSERT INTO Orders(order_num, order_date, cust_id) VALUES(20100,'2001/12/1','1000000010'); IF @@ERROR <> 0 ROLLBACK TRANSACTION StartOrder; 

Is it possible to implement something similar for MySQL? PS: I do not use PHP.

  • Можно ли реализовать что-то подобное для MySQL? Of course. For this there is a CREATE HANDLER. - Akina

1 answer 1

Here, for example, a small script that checks a request for an error, and then can perform the action you need.

 function q($page,$query){ // $page $result = mysql_query($query); if (mysql_errno()) { $error = "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$query\n<br>"; $log = mysql_query("INSERT INTO db_errors (error_page,error_text) VALUES ('$page','".escape_data($error)."')"); } }