Plsql 简明教程

PL/SQL - Loops

在本章中,我们将讨论 PL/SQL 中的循环。在很多情况下,需要多次执行代码块。通常,语句按顺序执行:函数中的第一个语句最先执行,然后是第二个,依此类推。

In this chapter, we will discuss Loops in PL/SQL. There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

编程语言提供了各种控制结构,允许执行更复杂的路径。

Programming languages provide various control structures that allow for more complicated execution paths.

循环语句允许我们多次执行一个语句或一组语句,以下是大多数编程语言中循环语句的一般形式 −

A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages −

loop architecture

PL/SQL 提供以下类型的循环来处理循环要求。点击以下链接查看其详细信息。

PL/SQL provides the following types of loop to handle the looping requirements. Click the following links to check their detail.

S.No

Loop Type & Description

1

PL/SQL Basic LOOPIn this loop structure, sequence of statements is enclosed between the LOOP and the END LOOP statements. At each iteration, the sequence of statements is executed and then control resumes at the top of the loop.

2

PL/SQL WHILE LOOPRepeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

3

PL/SQL FOR LOOPExecute a sequence of statements multiple times and abbreviates the code that manages the loop variable.

4

Nested loops in PL/SQLYou can use one or more loop inside any another basic loop, while, or for loop.

Labeling a PL/SQL Loop

可以标记 PL/SQL 循环。该标签应由双尖括号(<< 和 >>)括起来,并出现在 LOOP 语句的开头。标签名称也可以出现在 LOOP 语句的末尾。您可以在 EXIT 语句中使用该标签退出循环。

PL/SQL loops can be labeled. The label should be enclosed by double angle brackets (<< and >>) and appear at the beginning of the LOOP statement. The label name can also appear at the end of the LOOP statement. You may use the label in the EXIT statement to exit from the loop.

以下程序说明了这个概念 −

The following program illustrates the concept −

DECLARE
   i number(1);
   j number(1);
BEGIN
   << outer_loop >>
   FOR i IN 1..3 LOOP
      << inner_loop >>
      FOR j IN 1..3 LOOP
         dbms_output.put_line('i is: '|| i || ' and j is: ' || j);
      END loop inner_loop;
   END loop outer_loop;
END;
/

当以上代码在 SQL 提示符下执行时,它会生成以下结果:

When the above code is executed at the SQL prompt, it produces the following result −

i is: 1 and j is: 1
i is: 1 and j is: 2
i is: 1 and j is: 3
i is: 2 and j is: 1
i is: 2 and j is: 2
i is: 2 and j is: 3
i is: 3 and j is: 1
i is: 3 and j is: 2
i is: 3 and j is: 3

PL/SQL procedure successfully completed.

The Loop Control Statements

循环控制语句改变了它在正常序列中的执行。当执行退出一个作用域时,在该作用域中创建的所有自动对象会被销毁。

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

PL/SQL 支持以下控制语句。标记循环还有助于控制循环外部。点击以下链接查看其详细信息。

PL/SQL supports the following control statements. Labeling loops also help in taking the control outside a loop. Click the following links to check their details.

S.No

Control Statement & Description

1

EXIT statementThe Exit statement completes the loop and control passes to the statement immediately after the END LOOP.

2

CONTINUE statementCauses the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

3

GOTO statementTransfers control to the labeled statement. Though it is not advised to use the GOTO statement in your program.