Xavier Poinas
Xavier Poinas

Reputation: 19733

Stack conditions in CASE statement in PL/SQL

I'm using Oracle 10g and I'm trying to "stack" the conditions in a CASE statement, like I would do in C++ :

case 1:
case 2:
    // instructions
    break;

i.e. having the same code block executed for two different successful conditions.

I've tried :

WHEN 1, 2 THEN
WHEN 1 OR 2 THEN

... without luck. Is it even possible ?

EDIT - Full snippet

CASE v_n
  WHEN (1 OR 2) THEN
    dbms_output.put_line('Case 1 or 2');
  WHEN 3 THEN
    dbms_output.put_line('Case 3'); 
END CASE;

Generates an expression is of wrong type error

Upvotes: 13

Views: 46657

Answers (2)

Dima Korobskiy
Dima Korobskiy

Reputation: 1556

It is possible to use IN just as in SQL:

BEGIN
  CASE 
    WHEN EXTRACT(YEAR FROM SYSDATE) IN (2015, 2016, 2017) THEN
      dbms_output.put_line('Yes');
    ELSE
      dbms_output.put_line('No');
  END CASE;
END;
/

Upvotes: 2

WW.
WW.

Reputation: 24301

You need to use this format:

CASE
  WHEN v_n = 1 OR v_n = 2 THEN
    dbms_output.put_line('Case 1 or 2');
  WHEN v_n = 3 THEN
    dbms_output.put_line('Case 3'); 
END CASE;

Upvotes: 28

Related Questions