PL/SQL - Searched CASE 语句



被搜索的 CASE 语句没有选择器,该语句的 WHEN 子句包含提供布尔值的搜索条件。

语法

PL/SQL 中 searched CASE 语句的语法为 -

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

流程图

case statement in PL/SQL

示例

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 提示符处执行上述代码时,会产生以下结果 -

Very good  

PL/SQL procedure successfully completed. 
plsql_conditional_control.htm
广告