Home > Software engineering >  PL/SQL Case WHEN with two constants followed by THEN expression
PL/SQL Case WHEN with two constants followed by THEN expression

Time:01-28

is it possible for somehow having a CASE with an OR option like below in PL/SQL ?

CASE selector 
   WHEN 'value1' OR 'value2' THEN S1; 
   WHEN 'value3' OR 'value4' THEN S2; 
   WHEN 'value3' THEN S3; 
   ... 
   ELSE Sn;  -- default case 
END CASE;

CodePudding user response:

I'd rather use IN:

SQL> declare
  2    selector varchar2(20);
  3    result   varchar2(20);
  4  begin
  5    selector := '10';
  6
  7    result := case when selector in ('10', '20') then 's1'
  8                   when selector in ('30', '40') then 's2'
  9                   else 'sn'
 10              end;
 11    dbms_output.put_line('Result = ' || result);
 12  end;
 13  /
Result = s1

PL/SQL procedure successfully completed.

SQL>

You can use OR, though:

SQL> declare
  2    selector varchar2(20);
  3    result   varchar2(20);
  4  begin
  5    selector := '10';
  6
  7    result := case when selector = '10' or selector = '20' then 's1'
  8                   when selector = '30' or selector = '40' then 's2'
  9                   else 'sn'
 10              end;
 11    dbms_output.put_line('Result = ' || result);
 12  end;
 13  /
Result = s1

PL/SQL procedure successfully completed.

SQL>
  • Related