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;
这种语法在条件1为TRUE时执行一组语句,在条件2为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;
当条件为真时,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.