Monday, October 5, 2015

plsql - CASE STATEMENT

Like the IF statement, the CASE statement selects one sequence of statements to execute. However, to select the sequence, the CASE statement uses a selector rather than multiple Boolean expressions. A selector is an expression, whose value is used to select one of several alternatives.

Syntax:


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


Sample code:


DECLARE
  grade varchar2(1) :='&grade';
BEGIN
  CASE grade
  WHEN 'A' THEN
    dbms_output.put_line ('your grade is A as your score is above 70');
  WHEN 'B' THEN
    dbms_output.put_line ('your grade is B as your score is above 60 and below 70');
      WHEN 'C' THEN
    dbms_output.put_line ('your grade is C as your score is above 50 and below 60');
      WHEN 'D' THEN
    dbms_output.put_line ('your grade is D as your score is above 40 and below 50');
  END CASE;
END;

No comments:

Post a Comment