
- 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
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 Articles
- MySQL Sum Query with IF Condition using Stored Procedure
- Display records from MySQL stored Procedure with IF…THEN…END IF statements
- Display table records from a stored procedure in MySQL
- Can we update MySQL with if condition?
- How can I create a stored procedure to update values in a MySQL table?
- MySQL query to find single value from duplicates with certain condition by excluding other records using NOT IN
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- Create a stored procedure with delimiter in MySQL
- Create a Stored Procedure with MySQL and set a limit to display only a specific number of records
- Can we use stored procedure to insert records into two tables at once in MySQL?
- MySQL stored procedure return value?
- MySQL stored-procedure: out parameter?
- MySQL queries to update date records with NULL values
- How to select records beginning with certain numbers in MySQL?
- MySQL Stored Procedure to create a table?

Advertisements