PL/SQL教程

PL/SQL If

PL/SQL支持诸如条件语句和迭代语句之类的编程语言功能。它的编程结构类似于您在Java和C ++等编程语言中的使用方式。
IF语句的语法:
IF-THEN-ELSE语句的语法不同。
语法:(IF-THEN语句):
if condition 
THEN 
Statement: {It is executed when condition is true}
END IF;
仅当条件为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;
这是最高级的语法,如果在condition1为TRUE时要执行一组语句,在condition2为TRUE时要执行另一组语句,或者在condition1和condition2都为FALSE时要执行另一组语句,则使用
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. 
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4