Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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)
Advertisements
