Thursday, August 20, 2015

PL/SQL CASE Statement

This is an alternative to if… then… else… elsif… end if when you want to use conditional statements in PL/SQL. Here is a simple example, which checks whether a given number is 1 or 2. You start with the word case. Then you add one or more conditions followed by the action to take if that condition is satisfied. Each condition is preceded by the word when. You finish with the words end case:

SQL> declare
  2    one_or_two number := 1;
  3  begin
  4    case
  5    when one_or_two = 1 then
  6      dbms_output.put_line('One');
  7    when one_or_two = 2 then
  8      dbms_output.put_line('Two');
  9    end case;
 10  end;
 11  /
One
 
PL/SQL procedure successfully completed.
 
SQL>

The next example puts the variable name after the word case. This saves you having to include it in every when line:

SQL> declare
  2    one_or_two number := 2;
  3  begin
  4    case one_or_two
  5    when 1 then dbms_output.put_line('One');
  6    when 2 then dbms_output.put_line('Two');
  7    end case;
  8  end;
  9  /
Two
 
PL/SQL procedure successfully completed.
 
SQL>

If none of the when clauses are satisfied, you get an ORA-06592:

SQL> declare
  2    one_or_two number := 3;
  3  begin
  4    case
  5    when one_or_two = 1 then
  6      dbms_output.put_line('One');
  7    when one_or_two = 2 then
  8      dbms_output.put_line('Two');
  9    end case;
 10  end;
 11  /
declare
*
ERROR at line 1:
ORA-06592: CASE not found while executing CASE
statement
ORA-06512: at line 4
 
SQL>

One way to avoid this is to add an else clause before the end case:

SQL> declare
  2    one_or_two number := 3;
  3  begin
  4    case one_or_two
  5    when 1 then dbms_output.put_line('One');
  6    when 2 then dbms_output.put_line('Two');
  7    else dbms_output.put_line('Try again');
  8    end case;
  9  end;
 10  /
Try again
 
PL/SQL procedure successfully completed.
 
SQL>

An alternative is to add an exception section and check for case_not_found:

SQL> declare
  2    one_or_two number := 3;
  3  begin
  4    case
  5    when one_or_two = 1 then
  6      dbms_output.put_line('One');
  7    when one_or_two = 2 then
  8      dbms_output.put_line('Two');
  9    end case;
 10  exception
 11    when case_not_found then
 12      dbms_output.put_line('Try again');
 13  end;
 14  /
Try again
 
PL/SQL procedure successfully completed.
 
SQL>

Once a condition has been satisfied, Oracle jumps to the end case statement, ignoring the remaining conditions:

SQL> declare
  2    one_or_two number := 1;
  3  begin
  4    case
  5    when one_or_two = 1 then
  6      dbms_output.put_line('One');
  7    when one_or_two < 2 then
  8      dbms_output.put_line('Less than two');
  9    end case;
 10  exception
 11    when case_not_found then
 12      dbms_output.put_line('Try again');
 13  end;
 14  /
One
 
PL/SQL procedure successfully completed.
 
SQL>

You can even use a Boolean expression after the word case, as you can see in the two examples below:

SQL> begin
  2  case 1 = 1
  3  when true then
  4    dbms_output.put_line('1 = 1');
  5  when false then
  6    dbms_output.put_line('1 != 1');
  7  end case;
  8  end;
  9  /
1 = 1
 
PL/SQL procedure successfully completed.
 
SQL> begin
  2  case 1 = 2
  3  when true then
  4    dbms_output.put_line('1 = 2');
  5  when false then
  6    dbms_output.put_line('1 != 2');
  7  end case;
  8  end;
  9  /
1 != 2
 
PL/SQL procedure successfully completed.
 
SQL>

These examples were all tested in an Oracle 11 database.

No comments:

Post a Comment