PL/SQL可搜尋CASE語句

2019-10-16 22:54:15

可搜尋的CASE語句沒有選擇器,語句中的WHEN子句包含給出布林值的搜尋條件。

語法

PL/SQL中可搜尋的case語句的語法是 -

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

流程圖

範例

請參考以下範例程式碼 -

SET SERVEROUTPUT ON SIZE 1000000;
DECLARE 
   grade char(1) := 'B'; 
BEGIN 
   case  
      when grade = 'A' then dbms_output.put_line('Excellent'); 
      when grade = 'B' then dbms_output.put_line('Very good'); 
      when grade = 'C' then dbms_output.put_line('Well done'); 
      when grade = 'D' then dbms_output.put_line('You passed'); 
      when grade = 'F' then dbms_output.put_line('Better try again'); 
      else dbms_output.put_line('No such grade'); 
   end case; 
END; 
/

當上述程式碼在SQL提示符下執行時,它會產生以下結果 -