我们如何在MySQL存储过程中执行COMMIT事务?
众所周知,START事务将启动事务,而COMMIT用于在启动事务后进行任何更改。在以下示例中,我们使用COMMIT和START事务创建了一个存储过程,该过程将插入一条新记录,并在具有以下数据的表'employee.tbl'中提交更改-
mysql> Select * from employee.tbl; +----+---------+ | Id | Name | +----+---------+ | 1 | Mohan | | 2 | Gaurav | | 3 | Rahul | | 4 | Saurabh | +----+---------+ 4 rows in set (0.00 sec)
示例
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 //
现在,当我们调用此过程时,它将插入并更新表employee.tbl中的值。它还将提交更改。
mysql> Delimiter ; mysql> Call st_transaction_commit(); mysql> Select * from employee.tbl; +----+---------+ | Id | Name | +----+---------+ | 1 | Mohan | | 2 | Gaurav | | 3 | Sohan | | 4 | Saurabh | | 5 | Yash | +----+---------+ 5 rows in set (0.00 sec)