Home > Database > Mysql Tutorial > body text

MySQL 事务 ROLLBACK 示例代码

WBOY
Release: 2016-06-07 14:54:27
Original
1491 people have browsed it

无详细内容 事务 ROLLBACK MySQL mysqlmysql delimiter $$mysqlmysql CREATE PROCEDURE myProc( - in_from INTEGER, - in_to INTEGER, - in_amount DECIMAL(8,2)) - BEGIN - - DECLARE txn_error INTEGER DEFAULT 0 ; - - DECLARE CONTINUE HANDLER FOR SQLEX

事务 ROLLBACK MySQL
mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc(
    ->   in_from INTEGER,
    ->   in_to   INTEGER,
    ->   in_amount DECIMAL(8,2))
    -> BEGIN
    ->
    ->   DECLARE txn_error INTEGER DEFAULT 0 ;
    ->
    ->   DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN
    ->     SET txn_error=1;
    ->   END;
    ->
    ->   SAVEPOINT savepoint_tfer;
    ->
    ->   UPDATE employee
    ->      SET salary=in_amount
    ->    WHERE id=in_from;
    ->
    ->   IF txn_error THEN
    ->     ROLLBACK TO savepoint_tfer;
    ->     SELECT 'Transfer aborted ';
    ->   ELSE
    ->     UPDATE employee
    ->        SET salary=in_amount
    ->      WHERE id=in_to;
    ->
    ->      IF txn_error THEN
    ->         ROLLBACK TO savepoint_tfer;
    ->         SELECT 'Transfer aborted ';
    ->      END IF;
    ->   END IF;
    ->
    -> END$$
Query OK, 0 rows affected (0.00 sec)
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!