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)