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






mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc(customer_status VARCHAR(10))
    -> BEGIN
    ->
    ->     CASE customer_status
    ->          WHEN 'PLATINUM'  THEN
    ->               select 'PLATINUM';
    ->
    ->          WHEN 'GOLD' THEN
    ->               select 'GOLD';
    ->
    ->          WHEN 'SILVER' THEN
    ->               select 'SILVER';
    ->
    ->          WHEN 'BRONZE' THEN
    ->               select 'BRONZE';
    ->     END CASE;
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql> call myProc('BRONZE');
+--------+
| BRONZE |
+--------+
| BRONZE |
+--------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> call myProc('GOLD');
+------+
| GOLD |
+------+
| GOLD |
+------+
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)








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