Is there an equivalent SQL Server @@ error in MySQL?

I want to run an update request for a production database and as a good little developer, I try to make it as safe as possible. I want to do the following

BEGIN TRANSACTION
    UPDATE table_x SET col_y = 'some_value'
    .
    .
    .
IF (@@error <> 0)
BEGIN
    ROLLBACK
END
ELSE
BEGIN
    COMMIT
END

The above should work in SQL Server, but I need it to work with a MySQL database.

EDIT: Sorry, there are more than 1 instructions to follow. Yes, I know that you do not need to wrap one request in a transaction.

+3
source share
3 answers

I do not think this is necessary since there is a concept of implicit commit / rollback.

From MySQL Docs :

MySQL autocommit, MySQL SQL, . , . . 13.6.13, " InnoDB".

0
CREATE PROCEDURE prc_test()
BEGIN
  DECLARE EXIT HANDLER FOR SQLEXCEPTION
  BEGIN
    ROLLBACK;
  END;
  START TRANSACTION;
    INSERT
    INTO t_test VALUES ('test', 'test');
    INSERT
    INTO no_such_table
    VALUES ('no');
  COMMIT;
END;

CALL prc_test();

SELECT *
FROM t_test;

0 rows fetched.
+1
BEGIN;
UPDATE foo SET bar = 3;
UPDATE bar SET thing = 5;
COMMIT;

, . ROLLBACK, - .


MySQL, . . docs DECLARE HANDLER. , , SQLEXCEPTION. .

, , - , , . , MySQL ( ).

BEGIN
  DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
  START TRANSACTION;
    UPDATE foo SET bar = 3;
    UPDATE bar SET thing = 5;
  COMMIT;
END;

:

, . ; , - .

, "" , "" MySQL. - "", . , COMMIT, ROLLBACK .

, MySQL - , ( - ). MySQL , .

Finally, you can completely disable this behavior, and then you must constantly use transactions. I believe that "BEGIN" has been implied since the last execution or rollback, but you must either COMMIT or ROLLBACK any requests you requested.

For more information, see the InnoDB Transaction Model in the MySQL Guide.

0
source

Source: https://habr.com/ru/post/1703266/


All Articles