

- 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
MySQL Stored Procedure to update records with certain condition?
For this, you can use the UPDATE command along with the WHERE clause in a PROCEDURE. Let us first create a table −
mysql> create table DemoTable -> ( -> Id int, -> FirstName varchar(20), -> LastName varchar(20) -> ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(101,'David','Brown'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(102,'Chris','Brown'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(103,'John','Doe'); Query OK, 1 row affected (0.07 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+-----------+----------+ | Id | FirstName | LastName | +------+-----------+----------+ | 101 | David | Brown | | 102 | Chris | Brown | | 103 | John | Doe | +------+-----------+----------+ 3 rows in set (0.00 sec)
Here is the query to create a stored procedure−
mysql> delimiter // mysql> create procedure update_sp(fName varchar(20),lName varchar(20)) -> begin -> update DemoTable -> set FirstName=fName, -> LastName=lName -> where Id=101; -> end -> // Query OK, 0 rows affected (0.12 sec) mysql> delimiter ;
Now you can call a stored procedure using call command −
mysql> call update_sp('Adam','Smith'); Query OK, 1 row affected, 2 warnings (0.08 sec)
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+------+-----------+----------+ | Id | FirstName | LastName | +------+-----------+----------+ | 101 | Adam | Smith | | 102 | Chris | Brown | | 103 | John | Doe | +------+-----------+----------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL Sum Query with IF Condition using Stored Procedure
- Display table records from a stored procedure in MySQL
- Can we update MySQL with if condition?
- Display records from MySQL stored Procedure with IF…THEN…END IF statements
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- MySQL stored procedure return value?
- MySQL stored-procedure: out parameter?
- Create a stored procedure with delimiter in MySQL
- MySQL UPDATE using IF condition
- How to suppress MySQL stored procedure output?
- MySQL Stored Procedure to create a table?
- MySQL query to find single value from duplicates with certain condition by excluding other records using NOT IN
- How to select records beginning with certain numbers in MySQL?
- How can I create a stored procedure to update values in a MySQL table?
- Working with WHERE IN() in a MySQL Stored Procedure
Advertisements