PL/SQL - 嵌套的 IF-THEN-ELSE 语句

  • 简述

    在 PL/SQL 编程中嵌套 IF-ELSE 语句,这意味着您可以使用一个 IF 要么 ELSE IF 在另一个内部声明 IF 要么 ELSE IF 声明。
  • 句法

    
    IF( boolean_expression 1)THEN 
       -- executes when the boolean expression 1 is true  
       IF(boolean_expression 2) THEN 
          -- executes when the boolean expression 2 is true  
          sequence-of-statements; 
       END IF; 
    ELSE 
       -- executes when the boolean expression 1 is not true 
       else-statements; 
    END IF; 
    
  • 例子

    
    DECLARE 
       a number(3) := 100; 
       b number(3) := 200; 
    BEGIN 
       -- check the boolean condition  
       IF( a = 100 ) THEN 
       -- if condition is true then check the following  
          IF( b = 200 ) THEN 
          -- if condition is true then print the following  
          dbms_output.put_line('Value of a is 100 and b is 200' ); 
          END IF; 
       END IF; 
       dbms_output.put_line('Exact value of a is : ' || a ); 
       dbms_output.put_line('Exact value of b is : ' || b ); 
    END; 
    / 
    
    在 SQL 提示符下执行上述代码时,会产生以下结果 -
    
    Value of a is 100 and b is 200 
    Exact value of a is : 100 
    Exact value of b is : 200  
    PL/SQL procedure successfully completed.