Using WHILE LOOP in Oracle

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

Description

In Oracle, you use a WHILE LOOP when you are no longer sure how many instances you will execute the loop body and the loop physique may additionally now not execute even once.

Syntax

The syntax for the WHILE Loop in Oracle/PLSQL is:

WHILE condition
LOOP
   {...statements...}
END LOOP;

Parameters or Arguments

condition The situation is examined every pass by through the loop. If condition evaluates to TRUE, the loop body is executed. If circumstance evaluates to FALSE, the loop is terminated. statements The statements of code to execute every omit via the loop.

Note

You would use a WHILE LOOP statement when you are not sure of how many times you choose the loop physique to execute. Since the WHILE condition is evaluated earlier than getting into the loop, it is feasible that the loop physique may also not execute even once.

Example

Let’s look at a WHILE LOOP example in Oracle:

WHILE monthly_value <= 4000
LOOP
   monthly_value := daily_value * 31;
END LOOP;

In this WHILE LOOP example, the loop would terminate as soon as the monthly_value surpassed 4000 as special by:

WHILE monthly_value <= 4000

The WHILE LOOP will proceed whilst monthly_value <= 4000. And as soon as monthly_value is > 4000, the loop will terminate.