IF-THEN语句的序列之后的ELSE语句的可选序列,ELSE语句块在IF条件为FALSE时执行。

语法

IF-THEN-ELSE语句的语法是 -

IF condition THEN 
   S1;  
ELSE  
   S2; 
END IF;

其中,S1S2是不同的语句序列。 在IF-THEN-ELSE语句中,当测试条件为TRUE时,执行语句S1并跳过S2; 当测试条件为FALSE时,则跨过S1并执行语句S2中的语句块。 例如 -

IF color = red THEN 
  dbms_output.put_line('You have chosen a red car') 
ELSE 
  dbms_output.put_line('Please choose a color for your car'); 
END IF;

如果布尔表达式条件求值为真,则将执行if-then代码块,否则将执行else代码块。

流程图 -

示例

请看下面一个例子,演示如何使用 -

SET SERVEROUTPUT ON SIZE 1000000;
DECLARE 
   a number(3) := 100; 
BEGIN 
   -- check the boolean condition using if statement  
   IF( a < 20 ) THEN 
      -- if condition is true then print the following   
      dbms_output.put_line('a is less than 20 ' ); 
   ELSE 
      dbms_output.put_line('a is not less than 20 ' ); 
   END IF; 
   dbms_output.put_line('value of a is : ' || a); 
END; 
/

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


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

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