PL / SQL 中的条件语句

2024 年 8 月 29 日 | 阅读 12 分钟

在许多情况下,我们都需要做出选择,例如“是”或“否”。这些类型的问题也被称为决策语句。在这里,用户被迫选择一个答案。在这种情况下,我们根据我们所处的状态(条件)来选择答案。同样,每个程序都有类似的情况,比如如果这个条件为真,我们就选择“是”语句,如果条件为假,我们就选择“否”语句,在编写代码时也是如此。

每种编程语言都有一些条件语句。这些条件语句也被称为决策语句。PL / SQL(过程语言 / 结构化查询语言)中有一些条件语句或决策语句。

条件语句只能在 BEGIN 状态下使用。

条件语句的类型

  1. if condition then statement
  2. if condition then else statement
  3. 嵌套 if condition then statements
  4. if condition then elseif condition then else statements

1.) if Condition then Statement

这是第一个重要的 PL / SQL 条件语句之一。在此语句中,只有当条件为真时,才会显示输出。在任何其他情况下,都不会有输出。

语法

示例查询

输出

PL/SQL procedure successfully completed.

输出

The Value is 10

PL/SQL procedure successfully completed.

输出

Enter value for n1: 5
old   4: N1:=&N1;
new   4: N1:=5;

PL/SQL procedure successfully completed.

输出

Enter value for n1: 15
old   4: N1:=&N1;
new   4: N1:=15;
The Value is 15

PL/SQL procedure successfully completed.

输出

Enter value for n1: 9
old   4:     N1:=&N1;
new   4:     N1:=9;

PL/SQL procedure successfully completed.

输出

Enter value for n1: 10
old   4:     N1:=&N1;
new   4:     N1:=10;
The Value is 10

PL/SQL procedure successfully completed.

输出

Enter value for n1: 23
old   4:     N1:=&N1;
new   4:     N1:=23;
The Value is 23

PL/SQL procedure successfully completed.

输出

Enter value for n1: 5
old   6:     N1:=&N1;
new   6:     N1:=5;
Enter value for n2: 4
old   7:     N2:=&N2;
new   7:     N2:=4;
The Value is 20

PL/SQL procedure successfully completed.

输出

Enter value for n1: 9
old   6:     N1:=&N1;
new   6:     N1:=9;
Enter value for n2: 11
old   7:     N2:=&N2;
new   7:     N2:=11;

PL/SQL procedure successfully completed.

输出

Enter value for n1: 10
old   6:     N1:=&N1;
new   6:     N1:=10;
Enter value for n2: 10
old   7:     N2:=&N2;
new   7:     N2:=10;
The Value is 100

PL/SQL procedure successfully completed.

输出

Enter value for n1: 12
old   6:     N1:=&N1;
new   6:     N1:=12;
Enter value for n2: 2
old   7:     N2:=&N2;
new   7:     N2:=2;
The Value is 24

PL/SQL procedure successfully completed.

2.) if Condition then else Statement

这也是第一个重要的 PL / SQL 条件语句之一。在此语句中,只有当条件为真时,才会显示输出。在任何其他情况下,都有机会看到输出。如果 if 条件失败,则会执行 else 语句。

这意味着如果条件为真,则执行 If 语句。如果不是,则执行 else 语句。

这种条件语句需要用于解决一些现实生活中的问题,例如查找奇数或偶数、检查数字是否能被二、三等整除、查找最大值、查找最小值等,还有许多类似的程序。

语法

示例查询

输出

The Values of N1 is greater than N2

PL/SQL procedure successfully completed.

输出

The Values of N2 is smaller than N1
PL/SQL procedure successfully completed.

输出

Enter value for n1: 15
old   6:     N1:=&N1;
new   6:     N1:=15;
Enter value for n2: 16
old   7:     N2:=&N2;
new   7:     N2:=16;

输出

The Values of N1 is smaller than N2

PL/SQL procedure successfully completed.

输出

Enter value for n1: 10
old   6:     N1:=&N1;
new   6:     N1:=10;
Enter value for n2: 6
old   7:     N2:=&N2;
new   7:     N2:=6;
The Values of N2 is smaller than N1

PL/SQL procedure successfully completed.

输出

Enter value for n1: 15
old   6:     N1:=&N1;
new   6:     N1:=15;
Enter value for n2: 26
old   7:     N2:=&N2;
new   7:     N2:=26;
The Values of N2 is greater than N1

PL/SQL procedure successfully completed.

输出

Enter value for n1: 12
old   6:     N1:=&N1;
new   6:     N1:=12;
Enter value for n2: 11
old   7:     N2:=&N2;
new   7:     N2:=11;
The Values of N1 is greater than N2
PL/SQL procedure successfully completed.


Enter value for n1: 5
old   6:     N1:=&N1;
new   6:     N1:=5;
ODD NUMBER

PL/SQL procedure successfully completed.


Enter value for n1: 6
old   6:     N1:=&N1;
new   6:     N1:=6;
EVEN NUMBER

PL/SQL procedure successfully completed.

3.) if condition elsif condition else statements

这也是第一个重要的 PL / SQL 条件语句之一。在此语句中,只有当条件为真时,才会显示输出。在任何其他情况下,都有机会看到输出。如果 if 条件失败,则会执行带有条件的 else if 语句。即使 else if 条件为假,也会执行 else 语句。

语法

示例查询

输出

NUM2 SMALLER THAN NUM1

PL/SQL procedure successfully completed.

输出

EQUAL

PL/SQL procedure successfully completed.

输出

NUM1 SMALLER THAN NUM2

PL/SQL procedure successfully completed.

输入

输出

EQUAL

PL/SQL procedure successfully completed.

输入

输出

NUM1 SMALLER THAN NUM2

PL/SQL procedure successfully completed.

输入

输出

NUM2 SMALLER THAN NUM1

PL/SQL procedure successfully completed.

4.) 嵌套 if condition then statements

这也是 PL / SQL 中使用的条件语句之一。在这里,我们将编写相同的 if else 语句。但在 if 语句的位置,我们将编写另一个 if condition else 语句。这些语句的运作方式与之前的 if else 语句相同。

语法

示例查询

输出

The greatest number is 17

PL/SQL procedure successfully completed.

输出

The greatest number is 12

PL/SQL procedure successfully completed.

输出

The greatest number is 10
PL/SQL procedure successfully completed.

输入

输出

The greatest number is 20

PL/SQL procedure successfully completed.

输入

输出

The greatest number is 16
PL/SQL procedure successfully completed.

输入

输出

The greatest number is 21

PL/SQL procedure successfully completed.

输入

输出

The greatest number is 306

PL/SQL procedure successfully completed.

这就是关于 PL / SQL 条件表达式的全部内容。