- 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
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