

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How can I create a stored procedure to update values in a MySQL table?
We can create a stored procedure with IN operator to update values in a MySQL table. To make it understand we are taking an example of a table named ‘student_info’ having the following data −
mysql> Select * from student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 110 | Rahul | Chandigarh | History | | 125 | Raman | Bangalore | Computers | +------+---------+------------+------------+ 4 rows in set (0.01 sec)
Now, by creating the procedure named ‘update_studentinfo’ as follow, we can update the values in ‘student_info’ table −
mysql> DELIMITER // ; mysql> Create Procedure Update_studentinfo ( IN p_id INT, IN p_address varchar(20)) -> BEGIN -> UPDATE student_info -> SET -> address = p_address WHERE id = p_id; -> END // Query OK, 0 rows affected (0.13 sec)
Now, invoke the procedure with the values we want to update in the table as follows −
mysql> Delimiter ; // mysql> CALL Update_studentinfo(125, 'Shimla'); Query OK, 1 row affected (0.14 sec) mysql> Select * from student_info; +------+---------+------------+------------+ | id | Name | Address | Subject | +------+---------+------------+------------+ | 101 | YashPal | Amritsar | History | | 105 | Gaurav | Jaipur | Literature | | 110 | Rahul | Chandigarh | History | | 125 | Raman | Shimla | Computers | +------+---------+------------+------------+ 4 rows in set (0.00 sec)
The above result set shows that the address of record having id = 125 updated in the table.
- Related Questions & Answers
- How can I create a stored procedure to insert values in a MySQL table?
- How can I create a stored procedure to delete values from a MySQL table?
- How can I create a MySQL stored procedure that returns multiple values from a MySQL table?
- MySQL Stored Procedure to create a table?
- How can I create a stored procedure to select values on the basis of some conditions from a MySQL table?
- How can I create MySQL stored procedure with IN parameter?
- How can I create MySQL stored procedure with OUT parameter?
- How can I create MySQL stored procedure with INOUT parameter?
- MySQL stored procedure to execute SHOW CREATE TABLE?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- How can we update values in a MySQL table?
- Create a stored procedure to get the detail of a particular MySQL table stored in a database?
- How can we alter a MySQL stored procedure?
- How can we drop a MySQL stored procedure?
- Create a table inside a MySQL stored procedure and insert a record on calling the procedure
Advertisements