
- 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
Check for NULL or empty variable in a MySQL stored procedure
To check for NULL or empty variable, use the IF condition. Let us create a stored procedure −
mysql> delimiter // mysql> create procedure checkingForNullDemo(Name varchar(20)) begin if Name is NULL OR Name='' then select 'Adam Smith'; else select Name; end if ; end // Query OK, 0 rows affected (0.00 sec) mysql> delimiter ;
Case 1
When NULL is passed. Call the stored procedure using call command
mysql> call checkingForNullDemo(NULL);
This will produce the following output −
+------------+ | Adam Smith | +------------+ | Adam Smith | +------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec)
Case 2
When a value is passed. Call the stored procedure using the call command
mysql> call checkingForNullDemo('John Doe');
This will produce the following output −
+----------+ | Name | +----------+ | John Doe | +----------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec)
- Related Articles
- Check whether a field is empty or null in MySQL?
- In MySQL stored procedures, how to check if a local variable is null?
- MySQL “not a variable or NEW pseudo-variable” message. What is this error in my Stored Procedure?
- How to check if field is null or empty in MySQL?
- Set a custom value for NULL or empty values in MySQL
- Conditional WHERE clause in MySQL stored procedure to set a custom value for NULL values
- Check for NULL or NOT NULL values in a column in MySQL
- How do I check if a column is empty or null in MySQL?
- How to check whether a stored procedure exist in MySQL?
- Check if a String is empty ("") or null in Java
- Checking for Null or Empty in Java.
- Stored procedure using variable in LIMIT expression?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- Check if a String is whitespace, empty ("") or null in Java
- How to use FOR LOOP in MySQL Stored Procedure?

Advertisements