Oracle Plsql Repeat Until Loop

Oracle / PLSQL: REPEAT UNTIL LOOP

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

Description

Oracle doesn't have a REPEAT UNTIL LOOP, but you can emulate one with a LOOP statement.

Syntax

The syntax for emulating a REPEAT UNTIL LOOP in Oracle/PLSQL is:

LOOP

   {...statements...}

   EXIT [ WHEN <strong>boolean_condition</strong> ];

END LOOP;

Parameters or Arguments

statements

The statements of code to execute each pass through the loop.

boolean_condition

Optional. It is the condition to terminate the loop.

Note

  • You would use an emulated REPEAT UNTIL LOOP when you do not know how many times you want the loop body to execute.
  • The REPEAT UNTIL LOOP would terminate when a certain condition was met.

Example

Let's look at an example of how to emulate a REPEAT UNTIL LOOP in Oracle/PLSQL:

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

In this example, we want the loop to repeat until monthly_value is greater than 4000, so we use the EXIT WHEN statement.

EXIT WHEN monthly_value > 4000;

Now, the LOOP would repeat until the monthly_value exceeded 4000.