- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
In MySQL stored procedures, how to check if a local variable is null?
For this, use COALESCE(). Let us implement a stored procedure to check if the local variable is null −
mysql> DELIMITER // mysql> CREATE PROCEDURE local_VariableDemo() BEGIN DECLARE value1 int; DECLARE value2 int; select value1,value2; select concat('After checking local variable is null the sum is = ',COALESCE(value1,0)+COALESCE(value2,0)); END // Query OK, 0 rows affected (0.19 sec) mysql> DELIMITER ;
Call the stored procedure using CALL command −
mysql> call local_VariableDemo();
This will produce the following output −
+--------+--------+ | value1 | value2 | +--------+--------+ | NULL | NULL | +--------+--------+ 1 row in set (0.00 sec) +-----------------------------------------------------------------------------------------------------+ | concat('After checking local variable is null the sum is = ',COALESCE(value1,0)+COALESCE(value2,0)) | +-----------------------------------------------------------------------------------------------------+ | After checking local variable is null the sum is = 0 | +-----------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) Query OK, 0 rows affected (0.03 sec)
- Related Articles
- Check for NULL or empty variable in a MySQL stored procedure
- How to check if a variable is NULL in C/C++?
- How to check if data is NULL in MySQL?
- What is stored procedure and how can we create MySQL stored procedures?
- Create a stored Procedures using MySQL Workbench?
- What are stored procedures? How to call stored procedures using JDBC program?
- How to check if field is null or empty in MySQL?
- Call Stored Procedures within a Stored Procedure with IF Logic?
- How can we grant a user to access all stored procedures in MySQL?
- How can we access tables through MySQL stored procedures?
- How to check if any value is Null in a MySQL table single row?
- How do I check if a column is empty or null in MySQL?
- How can we see the list of stored procedures and stored functions in a particular MySQL database?
- How to check if a variable is NaN in JavaScript?
- How to check if a variable is boolean in JavaScript?

Advertisements