📜  PL / SQL If

📅  最后修改于: 2020-11-12 01:32:26             🧑  作者: Mango

PL / SQL如果

PL / SQL支持编程语言功能,例如条件语句和迭代语句。它的编程结构类似于您在Java和C++等编程语言中的使用方式。

IF语句的语法:

IF-THEN-ELSE语句有不同的语法。

语法:(IF-THEN陈述式):

IF condition 
THEN 
Statement: {It is executed when condition is true}
END IF;

仅当condition为TRUE时才要执行语句时,才使用此语法。

语法:(IF-THEN-ELSE陈述式):

IF condition 
THEN
   {...statements to execute when condition is TRUE...}
ELSE
   {...statements to execute when condition is FALSE...}
END IF; 

如果条件为TRUE时要执行一组语句,而条件为FALSE时要执行另一组语句,则使用此语法。

语法:(IF-THEN-ELSIF陈述式):

IF condition1 
THEN
   {...statements to execute when condition1 is TRUE...}
ELSIF condition2 
THEN
   {...statements to execute when condition2 is TRUE...}
END IF;

当condition1为TRUE时要执行一组语句,而condition2为TRUE时要执行另一组语句时,将使用此语法。

语法:(IF-THEN-ELSIF-ELSE陈述式):

IF condition1 
THEN
   {...statements to execute when condition1 is TRUE...}
ELSIF condition2 
THEN
   {...statements to execute when condition2 is TRUE...}
ELSE
   {...statements to execute when both condition1 and condition2 are FALSE...}
END IF;

当发现条件为TRUE时,IF-THEN-ELSE语句将执行相应的代码,并且不再检查条件。
如果不满足任何条件,则将执行IF-THEN-ELSE语句的ELSE部分。
ELSIF和ELSE部分是可选的。

PL / SQL If语句示例

让我们举个例子来看一下整个概念:

DECLARE
   a number(3) := 500;
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提示符下执行上述代码后,您将获得以下结果:


a is not less than 20
value of a is : 500
PL/SQL procedure successfully completed.