Chapter 5. Iterative Processing with Loops

This chapter explores the iterative control structures of PL/SQL, otherwise known as loops , which let you execute the same code repeatedly. PL/SQL provides three different kinds of loop constructs:

  • The simple or infinite loop

  • The FOR loop (numeric and cursor)

  • The WHILE loop

Each type of loop is designed for a specific purpose with its own nuances, rules for use, and guidelines for high-quality construction. As we explain each loop, we’ll provide a table describing the following properties of the loop:

Property

Description

How the loop is terminated

A loop executes code repetitively. How do you make the loop stop executing its body?

When the test for termination takes place

Does the test for termination take place at the beginning or end of the loop? What are the consequences?

Reason to use this loop

What are the special factors you should consider to determine if this loop is right for your situation?

Loop Basics

Why are there three different kinds of loops? To provide you with the flexibility you need to write the most straightforward code to handle any particular situation. Most situations that require a loop could be written with any of the three loop constructs. If you do not pick the construct that is best suited for that particular requirement, however, you could end up having to write many additional lines of code. The resulting module would also be harder to understand and maintain.

Examples of Different Loops

To give you a feeling for the ...

Get Oracle PL/SQL Programming, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.