Select*fromemployee.tbl;+- ---+---------+|Id|Name |+----+---------+| 1|Mohan || 2"/> Select*fromemployee.tbl;+- ---+---------+|Id|Name |+----+---------+| 1|Mohan || 2">

Home >Database >Mysql Tutorial >How can we perform COMMIT transaction in MySQL stored procedure?

How can we perform COMMIT transaction in MySQL stored procedure?

WBOY
WBOYforward
2023-09-11 11:21:021270browse

我们如何在 MySQL 存储过程中执行 COMMIT 事务?

As we all know that START transaction will start a transaction while COMMIT is used to make any changes after starting the transaction. In the following example, we have created a stored procedure using COMMIT and START transactions that will insert a new record and commit the changes in the table "employee.tbl" with the following data -

mysql> Select * from employee.tbl;
+----+---------+
| Id | Name    |
+----+---------+
|  1 | Mohan   |
|  2 | Gaurav  |
|  3 | Rahul   |
|  4 | Saurabh |
+----+---------+
4 rows in set (0.00 sec)

Example

mysql> Delimiter //
mysql> Create Procedure st_transaction_commit()
    -> BEGIN
    -> START TRANSACTION;
    -> INSERT INTO employee.tbl(name) values ('Yash');
    -> UPDATE employee.tbl set name = 'Sohan' WHERE id = 3;
    -> COMMIT;
    -> END //
Query OK, 0 rows affected (0.03 sec)

Now when we call this procedure, it will insert and update the values ​​in the table employee.tbl. It will also commit the changes.

mysql> Delimiter ;
mysql> Call st_transaction_commit();
Query OK, 0 rows affected (0.17 sec)

mysql> Select * from employee.tbl;
+----+---------+
| Id | Name    |
+----+---------+
|  1 | Mohan   |
|  2 | Gaurav  |
|  3 | Sohan   |
|  4 | Saurabh |
|  5 | Yash    |
+----+---------+
5 rows in set (0.00 sec)

The above is the detailed content of How can we perform COMMIT transaction in MySQL stored procedure?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete