在PL/SQL中,IF-THEN-ELSIF语句允许在多种选择之间进行选择。IF-THEN语句后面可以有一个可选的ELSIF ... ELSE语句。 ELSIF子句可用于添加附加条件。

使用IF-THEN-ELSIF语句时需要注意几点。

  • 需要看清楚,它是ELSIF,并不是ELSEIF
  • IF-THEN语句可以有零个或一个ELSE,它必须在ELSIF之后。
  • IF-THEN语句可以有零或多个ELSIF,它们必须在ELSE之前。
  • 一旦有一个ELSIF条件测试成功,其余的ELSIFELSE都不会再被测试。

语法

PL/SQL编程语言中的IF-THEN-ELSIF语句的语法是 -

IF(boolean_expression 1)THEN  
   S1; -- Executes when the boolean expression 1 is true  
ELSIF( boolean_expression 2) THEN 
   S2;  -- Executes when the boolean expression 2 is true  
ELSIF( boolean_expression 3) THEN 
   S3; -- Executes when the boolean expression 3 is true  
ELSE  
   S4; -- executes when the none of the above condition is true  
END IF;

示例

参考以下示例代码 -

SET SERVEROUTPUT ON SIZE 1000000;
DECLARE 
   a number(3) := 100; 
BEGIN 
   IF ( a = 10 ) THEN 
      dbms_output.put_line('Value of a is 10' ); 
   ELSIF ( a = 20 ) THEN 
      dbms_output.put_line('Value of a is 20' ); 
   ELSIF ( a = 30 ) THEN 
      dbms_output.put_line('Value of a is 30' ); 
   ELSE 
       dbms_output.put_line('None of the values is matching'); 
   END IF; 
   dbms_output.put_line('Exact value of a is: '|| a );  
END; 
/

当上述代码在SQL提示符下执行时,它会产生以下结果 -


原文链接:https://www.yiibai.com/plsql/plsql_if_then_elsif.html

最后修改:2022 年 01 月 18 日
如果觉得我的文章对你有用,请随意赞赏