
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements