
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we perform COMMIT transactions inside MySQL stored procedure?
As we know the START transaction will start the transaction and COMMIT is used to made any changes made after starting the transaction. In the following example, we have created a stored procedure with COMMIT along with START transaction which will insert a new record and commit changes in table ‘employee.tbl’ having 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 invoke this procedure, it will insert and update the value in 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)
- Related Articles
- How can we perform START transactions inside MySQL stored procedure?
- How can we perform ROLLBACK transactions inside a MySQL stored procedure?
- How can we handle a result set inside MySQL stored procedure?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- How can we invoke MySQL stored procedure?
- How can we alter a MySQL stored procedure?
- How can we drop a MySQL stored procedure?
- What is stored procedure and how can we create MySQL stored procedures?
- How can we write MySQL handler in a stored procedure?
- How can we create MySQL stored procedure to calculate the factorial?
- Perform mathematical operations in a MySQL Stored Procedure?
- How can we use nested transactions in MySQL?
- Implement Dynamic SQL query inside a MySQL stored procedure?
- How can we see the source code of a particular MySQL stored procedure?
- What happens when we use COMMIT in MySQL stored procedure and one of the transaction, under START transaction, fails?

Advertisements