`

plsql中的循环

阅读更多

plsql的循环有三种类型:

• The simple or infinite loop
• The FOR loop (numeric and cursor)
• The WHILE loop

 

The simple loop
It’s called simple for a reason: it starts simply with the LOOP keyword and ends
with the END LOOP statement. The loop will terminate if you execute an EXIT,
EXIT WHEN, or RETURN within the body of the loop (or if an exception is raised):

PROCEDURE display_multiple_years (
start_year_in IN PLS_INTEGER
,end_year_in IN PLS_INTEGER
)
IS
l_current_year PLS_INTEGER := start_year_in;
BEGIN
LOOP
EXIT WHEN l_current_year > end_year_in;
display_total_sales (l_current_year);
l_current_year := l_current_year + 1;
END LOOP;
END display_multiple_years;

 

当不知道循环的范围,且至少循环一次时可用。

Property Description
How the loop is terminated The simple loop is terminated when an EXIT statement is executed in the body of the loop. If
this statement is not executed, the simple loop becomes a true infinite loop.
When the test for
termination takes place
The test takes place inside the body of the loop, and then only if an EXIT or EXIT WHEN statement
is executed. Therefore, the body—or part of the body—of the simple loop always executes
at least once.
Reason to use this loop Use the simple loop when:
• You are not sure how many times you want the loop to execute.
• You want the loop to run at least once.

 

The FOR loop
Oracle offers a numeric and cursor FOR loop. With the numeric FOR loop, you
specify the start and end integer values, and PL/SQL does the rest of the work for
you, iterating through each intermediate value, and then terminating the loop:

对于numeric for loop 

PROCEDURE display_multiple_years (
start_year_in IN PLS_INTEGER
,end_year_in IN PLS_INTEGER
)
IS
BEGIN
FOR l_current_year IN start_year_in .. end_year_in
LOOP
display_total_sales (l_current_year);
END LOOP;
END display_multiple_years;

当知道循环范围时可用,循环变量在loop范围内有效,为number类型,plsql隐式定义,会为其自动加1.

Property Description
How the loop is terminated The numeric FOR loop terminates unconditionally when the number of times specified in its range
scheme has been satisfied. You can also terminate the loop with an EXIT statement, but this is not
recommended.
When the test for
termination takes place
After each execution of the loop body, PL/SQL increments (or decrements if REVERSE is specified) the
loop index and then checks its value. When it exceeds the upper bound of the range scheme, the
loop terminates. If the lower bound is greater than the upper bound of the range scheme, the loop
never executes its body.
Reason to use this loop Use the numeric FOR loop when you want to execute a body of code a fixed number of times and do
not want to halt that looping prematurely.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

当要从游标或select语句取出全部的记录时,可用。循环变量类型为cursor_name%rowtype,plsql隐式定义。

用cursor for loop即简洁又清晰,如:

DECLARE
CURSOR occupancy_cur IS
SELECT pet_id, room_number
FROM occupancy WHERE occupied_dt = TRUNC (SYSDATE);
occupancy_rec occupancy_cur%ROWTYPE;
BEGIN
OPEN occupancy_cur;
LOOP
FETCH occupancy_cur INTO occupancy_rec;
EXIT WHEN occupancy_cur%NOTFOUND;
update_bill
(occupancy_rec.pet_id, occupancy_rec.room_number);
END LOOP;
CLOSE occupancy_cur;
END;

 而用cursor for loop,会省掉open cursor,fentch cursor,if cursor%notfound,close cursor一系列操作,都由plsql隐式完成了。游标的开关在循环中隐式完成。如:

DECLARE
CURSOR occupancy_cur IS
SELECT pet_id, room_number
FROM occupancy WHERE occupied_dt = TRUNC (SYSDATE);
BEGIN
FOR occupancy_rec IN occupancy_cur
LOOP
update_bill (occupancy_rec.pet_id, occupancy_rec.room_number);
END LOOP;
END;
Property Description
How the loop is terminated The cursor FOR loop terminates unconditionally when all of the records in the associated cursor
have been fetched. You can also terminate the loop with an EXIT statement, but this is not
recommended.
When the test for
termination takes place
After each execution of the loop body, PL/SQL performs another fetch. If the %NOTFOUND
attribute of the cursor evaluates to TRUE, then the loop terminates. If the cursor returns no
rows, then the loop never executes its body.
Reason to use this loop Use the cursor FOR loop when you want to fetch and process every record in a cursor.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The WHILE loop
The WHILE loop is very similar to the simple loop; a critical difference is that it
checks the termination condition up front. It may not even execute its body a single
time:

PROCEDURE display_multiple_years (
start_year_in IN PLS_INTEGER
,end_year_in IN PLS_INTEGER
)
IS
l_current_year PLS_INTEGER := start_year_in;
BEGIN
WHILE (l_current_year <= end_year_in)
LOOP
display_total_sales (l_current_year);
l_current_year := l_current_year + 1;
END LOOP;
END display_multiple_years;

while loop和simple loop差不多,只是根据具体的条件进行循环。

Property Description
How the loop is terminated The WHILE loop terminates when the Boolean expression in its boundary evaluates to FALSE
or NULL.
When the test for
termination takes place
The test for termination of a WHILE loop takes place in the loop boundary. This evaluation
occurs prior to the first and each subsequent execution of the body. The WHILE loop, therefore,
is not guaranteed to execute its loop even a single time.
Reason to use this loop Use the WHILE loop when:
• You are not sure how many times you must execute the loop body.
• You will want to conditionally terminate the loop.
• You don’t have to execute the body at least one time.
0
1
分享到:
评论

相关推荐

    PLSQL几种循环语句用法

    PLSQL几种循环语句用法:适合初级人员使用。

    PLSQL中文语言包

    PLSQL(Procedural Language/Structured Query Language)是Oracle数据库管理系统中的一个编程组件,它是SQL的面向过程的扩展,使得数据库管理员和开发人员能够编写复杂的数据库应用程序。在Oracle环境中,PLSQL提供...

    PLSQL最新中文手册

    在PLSQL中,你可以创建一系列的程序逻辑,如循环、条件判断、异常处理等,这些在单纯的SQL语句中通常是无法实现的。PLSQL的结构包括声明部分、执行部分和异常处理部分,这使得开发者可以构建复杂的业务逻辑。 ...

    oracle plsql for loop循环 求素数

    - **MOD函数**:与`LOOP`循环中的用法相同。 #### WHILE 循环 `WHILE`循环则是在满足特定条件时重复执行一段代码块。下面是如何使用`WHILE`循环来找出100到200之间的所有素数: ```plsql DECLARE n NUMBER := ...

    Oracle PLSQL存储过程循环LOOP跳处循环、退出循环、终止循环流程详解与实战指南

    5、高效性:通过学习资源中的循环控制方法,能够显著提高PL/SQL程序的运行效率和逻辑清晰度。 6、灵活性:丰富的控制策略使您能够根据不同业务需求灵活调整循环行为。 7、实用性强:所有示例均基于实际业务场景,...

    PLSQL 中文手册.7z

    3. **流程控制**:PLSQL提供了条件语句(IF-THEN-ELSIF-ELSE)、循环语句(WHILE、FOR)以及CASE语句,用于实现条件判断和循环逻辑。 4. **集合类型**:PLSQL支持集合数据类型,如数组(VARRAY)、列表(NESTED ...

    PLSQL中文详细使用教程 有标签可快速定位

    PLSQL,全称为Procedural Language/Structured Query Language,是Oracle数据库系统中用于操作和管理数据的编程语言。它结合了SQL(结构化查询语言)的数据库查询功能和一种过程式编程语言,使得数据库管理员和开发...

    最新完全中文PLSQL中文版

    3. **流程控制语句**:PLSQL包含IF-THEN-ELSIF-ELSE、CASE、FOR循环、WHILE循环等流程控制结构,允许根据条件执行不同的代码路径。 4. **集合和记录**:PLSQL支持集合类型,如VARrays和Associative Arrays,以及...

    PLSQL 操作学习文档

    PLSQL(Procedural Language/Structured Query Language)是Oracle数据库中的一个强大工具,它结合了SQL的查询功能和过程性编程语言的特点,使得数据库管理、数据处理和应用程序开发更为便捷。"PLSQL Developer"是一...

    plsql中文

    1. Oracle007(16-20).doc 和 Oracle007(9-15).doc:这些文档可能包含了PL/SQL的分步骤教程,从第9讲到第20讲,涵盖了PL/SQL的基本概念、语法、变量声明、控制结构(如IF-ELSE,WHILE,FOR循环)以及游标(CURSOR)的...

    PLSQL7.1经典版本

    PLSQL(Procedural Language/Structured Query Language)是Oracle数据库的一种扩展,它是SQL的编程语言形式,主要用于在Oracle环境中编写存储过程、函数、触发器等数据库应用程序。PLSQL7.1是PLSQL的一个经典版本,...

    PLSQL电子书小合集

    执行部分则包含SQL语句和PLSQL控制结构,如循环、条件判断等;异常处理部分帮助我们捕获和处理在程序运行过程中可能出现的问题。 PLSQL中的主要元素有: 1. **变量(Variables)**:用于存储数据,可以是数值、...

    PLSQL语言初级教程

    - **EXIT和CONTINUE**:在循环中跳出当前循环或跳过本次循环迭代。 4. **PLSQL函数与过程** - **内置函数**:了解Oracle提供的内置函数,如TO_CHAR、TO_DATE、COUNT、MAX、MIN等。 - **自定义函数和过程**:学习...

    oarcle管理工具plsql PLSQL ORACLE TOOL

    在Oracle的环境中,管理工具PLSQL Developer不仅简化了PL/SQL的开发流程,还提供了许多实用的功能,例如数据库连接管理、作业调度、性能分析等,使得数据库管理和维护工作变得更加便捷和高效。无论是新手还是经验...

    PLSQL 个人自学总结

    PLSQL,全称为“Procedural Language/Structured Query Language”,是Oracle数据库系统中用于扩展SQL的一种编程语言。它结合了SQL的查询能力与过程式编程的灵活性,使得开发者能够更高效地管理和操作数据库。在本篇...

    《Oracle PLSQL 从入门到精通》.zip

    Oracle PLSQL,全称为“Procedural Language/Structured Query Language”,是Oracle数据库系统中的一个强大编程语言,它结合了SQL的查询能力与过程性编程语言的功能。本资料《Oracle PLSQL 从入门到精通》旨在帮助...

    PLSQL求10的阶乘

    在数据库编程中,经常需要用到循环结构来处理数据或执行特定任务。阶乘是一个常见的示例,它不仅有助于理解循环语句的基本用法,还能够帮助我们更好地掌握数据库编程语言的语法结构。本文将详细介绍如何使用 PL/SQL ...

    PLSQL高级编程-结构化编程

    配合FOR循环,游标可以在循环中方便地访问和操作数据。 六、集合和记录 PLSQL支持两种类型的集合:索引数组(VARRAYs)和关联数组(Associative Arrays)。它们允许我们存储相同类型的多个元素。记录(RECORD)类型...

    PLSQL 11.03.1770

    PLSQL,全称为Procedural Language/Structured Query Language,是Oracle数据库系统中用于扩展SQL功能的一种编程语言。它集成了SQL查询语言与过程式编程语言的特性,为数据库开发提供了强大的工具。标题“PLSQL ...

    plsql用户指南与参考(中文翻译版)

    它允许在数据库中编写复杂的业务逻辑,包括循环、条件判断、异常处理等。 2. **声明变量**:在PL/SQL中,你可以声明各种类型的变量,如数值、字符串、日期等。这些变量可以用来存储和处理数据。 3. **块结构**:PL...

Global site tag (gtag.js) - Google Analytics