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

众所周知,START事务将启动该事务,并将自动提交模式设置为off。在以下示例中,我们使用START事务创建了一个存储过程,该过程将在表employee.tbl中插入一条具有以下数据的新记录-

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

示例

mysql> Delimiter //
mysql> Create Procedure st_transaction()
   -> BEGIN
   -> START TRANSACTION;
   -> INSERT INTO employee.tbl(name) values ('Saurabh');
   -> END //

现在,当我们调用此过程时,它将在表employee.tbl中插入值。

mysql> Delimiter ;
mysql> Call st_transaction();

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