Using Loop Statement in Oracle

This Oracle tutorial explains how to use the LOOP assertion in Oracle with syntax and examples.

Description

In Oracle, the LOOP declaration is used when you are no longer positive how many times you prefer the loop body to execute and you desire the loop body to execute at least once.

Syntax

The syntax for the LOOP announcement in Oracle/PLSQL is:

LOOP
   {...statements...}
END LOOP;

Parameters or Arguments

statements The statements of code to execute every ignore through the loop.

Note

You would use a LOOP declaration when you are not sure of how many instances you want the loop body to execute. You can terminate a LOOP announcement with either an EXIT assertion or when it encounters an EXIT WHEN assertion that evaluates to TRUE.

Example

Let’s look at a LOOP example in Oracle:

LOOP
   monthly_value := daily_value * 31;
   EXIT WHEN monthly_value > 4000;
END LOOP;

In this LOOP example, the LOOP would terminate when the monthly_value handed 4000.