 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set conditions in a MySQL stored procedure
To set conditions in a stored procedure, use IF...ELSE in MySQL. Following is the syntax for if-else −
IF yourCondition then yourStatement1, ELSE yourStatement2, END IF;
Let us implement the above syntax in a stored procedure −
mysql> DELIMITER // mysql> CREATE PROCEDURE IF_ELSE_DEMO(IN value int) -> BEGIN -> SET @val=value; -> IF @val > 10 then -> select concat(@val,' is greater than 10'); -> ELSE -> select concat(@val,' is less than 10 '); -> END IF; -> END; -> // Query OK, 0 rows affected (0.16 sec)
mysql> DELIMITER ;
Now you can call the stored procedure using CALL command −
mysql> call IF_ELSE_DEMO(18);
This will produce the following output −
+------------------------------------+ | concat(@val,' is greater than 10') | +------------------------------------+ | 18 is greater than 10 | +------------------------------------+ 1 row in set (0.03 sec) Query OK, 0 rows affected (0.04 sec)
Advertisements
 