
- 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
Conditional WHERE clause in MySQL stored procedure to set a custom value for NULL values
To set a custom value for NULL values, use the UPDATE command along with IS NULL property in a stored procedure. Let us first create a table −
mysql> create table DemoTable ( Id int, FirstName varchar(50) ); Query OK, 0 rows affected (0.67 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,'Chris'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(101,NULL); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(102,'Mike'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable values(103,NULL); Query OK, 1 row affected (0.74 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+-----------+ | Id | FirstName | +------+-----------+ | 100 | Chris | | 101 | NULL | | 102 | Mike | | 103 | NULL | +------+-----------+ 4 rows in set (0.00 sec)
Following is the query to create a stored procedure −
mysql> DELIMITER // mysql> create procedure where_clauseProc() BEGIN update DemoTable set FirstName='Robert' where FirstName IS NULL; END // Query OK, 0 rows affected (4.11 sec) mysql> DELIMITER ;
Call the stored procedure using CALL command −
mysql> call where_clauseProc(); Query OK, 2 rows affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------+-----------+ | Id | FirstName | +------+-----------+ | 100 | Chris | | 101 | Robert | | 102 | Mike | | 103 | Robert | +------+-----------+ 4 rows in set (0.00 sec)
- Related Articles
- Set a custom value for NULL or empty values in MySQL
- Implement Conditional MySQL Query in a stored procedure?
- Set value only for NULL values in a MySQL table
- Check for NULL or empty variable in a MySQL stored procedure
- Set conditions in a MySQL stored procedure
- Working with WHERE IN() in a MySQL Stored Procedure
- MySQL stored procedure return value?
- MySQL stored procedure to return a column value?
- MySQL query to set values for NULL occurrence
- Set multiple values for custom columns in MySQL?
- Set custom messages for enum values in MySQL
- Set 1 for NOT NULL value in MySQL
- Update with multiple values in MySQL WHERE clause
- Place a specific value for NULL values in a MySQL column
- Set an alternative of WHERE clause for each SELECT field in MySQL

Advertisements