There may arise a situation when you need to execute a particular block of code several number of times. Programming languages provide various control structures that allow for more complicated execution paths and Loops are one of them. In other words, you may sometimes need to run a particular block of code in the program for a specific number of times or until a certain condition is satisfied. For this purpose, Oracle PL/SQL provides us the Loop facility. These statements are executed sequentially one by one from top to bottom. Loop technique in PL/SQL is also known as iterative statements.

The following are the Iterative structures or Loops provided in Oracle PL/SQL.

PL/SQL Loops

PL/SQL General Loop

A General Loop in PL/SQL is used to execute a set of statements at least once before the termination of the loop. An EXIT condition has to be specified in the loop; otherwise the looping process will get into a never ending loop, also known as an Infinite Loop. When the EXIT condition in the loop is satisfied, the control exits from the loop.

In a PL/SQL Loop, the statements are enclosed between the keywords LOOP and END LOOP. In every Loop, the statements are executed and then control restarts from the top of the loop until a certain condition is satisfied.

Syntax

Alternatively, you can use an EXIT WHEN statement to exit from the Loop. If you use just an EXIT statement, the statements in the loop is executed only once.

Example

Output

PL / SQL循环

PL/SQL FOR Loop

A FOR LOOP is used to execute a series of statements for a specific number of times. Iteration occurs between the starting and ending integer values in a given range. The counter variable (which is mandatory) is always incremented by 1. The loop exits when the counter attains the value of the end integer.

Syntax

Here, the variable var1 represents the start value in the range and var2 represents the end value. The counter variable is implicitly declared in the declaration section, so it’s not necessary to declare it explicitly. The counter variable is automatically incremented by 1 and therefore does not need to be incremented explicitly. EXIT WHEN statement and EXIT statements can be optionally used in FOR loops.

Example

Output

PL/SQL WHILE Loop

This Loop repeats a series of statements while a given condition is true. Before entering the loop body, a condition in the while section is verified and if it turns out to be TRUE, then the control executed the loop until the condition becomes FALSE. It tests the condition before executing the loop body. The condition is verified at the beginning of each iteration. EXIT WHEN statement and EXIT statements can be used in a while loop.

Syntax

Example

Output

PL / SQL循环

PL/SQL Nested Loop

You can optionally use one or more loop inside another Simple Loop, While Loop or a FOR loop. This type of a structure is used to verify or evaluate two conditions simultaneously or one after another.

Control Statements in Loops

There are some control statements that are used inside or in association with the above mentioned loops to control the functioning of these loops and termination of these loops based on their conditions.

EXIT Statement When a loop encounters the term EXIT, it automatically terminates the loop and the control passes to the very next statement after the Loop.

EXIT-WHEN Statement The EXIT-WHEN statement allows a loop complete conditionally. When the EXIT-WHEN statement is encountered, the condition in the WHEN Block is evaluated. If the condition evaluates to be TRUE, the loop completes (or terminates) and control passes to the very next statement after the loop.

Example

These two statements are logically correct and equivalent, but the EXIT-WHEN statement is easier comparatively.

CONTINUE Statement This statement causes the loop to skip the remaining part of its lock and immediately re-evaluate its condition prior to reiterating or getting into the loop once again.

GOTO Statement This statement helps to transfer the control to a labeled statement in another part of the very same program. It is generally not advised to use GOTO statements in programs. A GOTO statement can’t be branched out into a sub-block, LOOP statement, an IF statement or a CASE statement. 

A GOTO statement cannot branch out of a sub-program. To end a sub-program early, you can use the RETURN statement or a GOTO statement to branch to a place right before the end of the sub-program.

当您需要多次执行特定代码块时,可能会出现这种情况。 编程语言提供了各种控制结构,允许使用更复杂的执行路径,而循环就是其中之一。 换句话说,有时您可能需要在程序中运行特定的代码块特定的次数或直到满足特定条件。 为此,Oracle PL / SQL向我们提供了Loop工具。 这些语句从上到下依次执行。 PL / SQL中的循环技术也称为迭代语句。

以下是Oracle PL / SQL中提供的迭代结构或循环。

PL / SQL循环

PL / SQL通用循环

PL / SQL中的通用循环用于在循环终止之前至少执行一次语句集。 必须在循环中指定退出条件; 否则,循环过程将陷入永无止境的循环,也称为无限循环。 当满足循环中的退出条件时,控制从循环中退出。

在PL / SQL循环中,语句包含在关键字LOOP和END LOOP之间。 在每个循环中,将执行语句,然后控制从循环顶部重新开始,直到满足特定条件为止。

句法

或者,您可以使用EXIT WHEN语句退出循环。 如果仅使用EXIT语句,则循环中的语句仅执行一次。

输出量

PL / SQL循环

PL / SQL FOR循环

FOR LOOP用于执行一系列语句特定次数。 迭代发生在给定范围内的起始整数值和终止整数值之间。 计数器变量(必填)始终增加1。当计数器达到结束整数的值时,循环退出。

句法

此处,变量var1代表范围内的起始值,变量var2代表范围内的终止值。 counter变量在声明部分中隐式声明,因此不必显式声明它。 计数器变量自动增加1,因此不需要显式增加。 EXIT WHEN语句和EXIT语句可以在FOR循环中选择使用。

输出量

PL / SQL WHILE循环

当给定条件为true时,此循环重复一系列语句。 在进入循环主体之前,将验证while部分中的条件,如果结果为TRUE,则控件将执行循环,直到条件变为FALSE。 它在执行循环体之前测试条件。 在每次迭代开始时都会验证条件。 EXIT WHEN语句和EXIT语句可以在while循环中使用。

句法

输出量

PL / SQL循环

PL / SQL嵌套循环

您可以选择在另一个简单循环,While循环或FOR循环中使用一个或多个循环。 这种类型的结构用于同时或一个接一个地验证或评估两个条件。

循环中的控制语句

在上述循环内部或与之关联使用一些控制语句,以根据这些循环的条件控制这些循环的功能以及这些循环的终止。

EXIT语句当循环遇到术语EXIT时,它将自动终止循环,并且控制权传递到循环之后的下一个语句。

EXIT-WHEN语句 EXIT-WHEN语句允许有条件地完成循环。 遇到EXIT-WHEN语句时,将评估WHEN块中的条件。 如果条件评估为TRUE,则循环完成(或终止),并且控制权传递到循环后的下一条语句。

这两个语句在逻辑上是正确的并且是等效的,但EXIT-WHEN语句相对来说比较容易。

CONTINUE语句此语句使循环跳过其锁的其余部分,并在重新重申或再次进入循环之前立即重新评估其条件。

GOTO语句此语句有助于将控件转移到同一程序的另一部分中的带标签的语句。 通常不建议在程序中使用GOTO语句。 GOTO语句不能分支到子块,LOOP语句,IF语句或CASE语句中。

GOTO语句不能从子程序中分支出来。 要提早结束子程序,可以使用RETURN语句或GOTO语句跳转到子程序结束之前的位置。

翻译自: https://www.thecrazyprogrammer.com/2015/07/plsql-loops.html

相关文章: