IF语句将条件与由关键字 THEN 和 END IF 括起来的一系列语句相关联,如果条件为 TRUE ,则执行语句,如果条件为 FALSE 或 NULL ,则 IF 语句不执行任何操作。
IF condition THEN S; END IF;
其中 condition 是布尔条件或关系条件,而S是简单或复合语句,以下是IF-THEN语句的示例-
IF (a <= 20) THEN c:= c+1; END IF;
让无涯教程尝试一个示例,以帮助您理解概念-
DECLARE a number(2) := 10; BEGIN a:= 10; -- 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 ' ); END IF; dbms_output.put_line('value of a is : ' || a); END; /
当以上代码在SQL提示符下执行时,将产生以下输出-
a is less than 20 value of a is : 10 PL/SQL procedure successfully completed.
考虑无涯教程在 PL/SQL变量类型中创建的表中有很少的记录。
DECLARE c_id customers.id%type := 1; c_sal customers.salary%type; BEGIN SELECT salary INTO c_sal FROM customers WHERE id=c_id; IF (c_sal <= 2000) THEN UPDATE customers SET salary= salary + 1000 WHERE id=c_id; dbms_output.put_line ('Salary updated'); END IF; END; /
当以上代码在SQL提示符下执行时,将产生以下输出-
Salary updated PL/SQL procedure successfully completed.
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Python实战 · 从0到1搭建直播视频平台 -〔Barry〕