Using CASE WHEN condition statement in a procedure : CASE « Procedure Function « MySQL Tutorial






mysql>
mysql> delimiter $$
mysql> CREATE PROCEDURE myProc(id int)
    -> BEGIN
    ->
    ->     CASE
    ->          WHEN id < 2  THEN
    ->               select 'less than 2';
    ->
    ->          WHEN id > 2 and id < 5 THEN
    ->               select 'greater than 2 and less than 5';
    ->
    ->          WHEN id > 5 THEN
    ->               select 'greater than 5';
    ->
    ->     END CASE;
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql> call myProc(2);
ERROR 1339 (20000): Case not found for CASE statement
mysql> call myProc(20);
+----------------+
| greater than 5 |
+----------------+
| greater than 5 |
+----------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> drop procedure myProc;
Query OK, 0 rows affected (0.00 sec)

mysql>








11.15.CASE
11.15.1.For checking a uniform condition you may use the CASE construct
11.15.2.CASE Statement with Condition Checks
11.15.3.Using CASE WHEN statement in a procedure
11.15.4.Using CASE WHEN condition statement in a procedure
11.15.5.CASE WHEN with ELSE