EXIT Statement

The EXIT statement exits a loop and transfers control to the end of the loop. The EXIT statement has two forms: the unconditional EXIT and the conditional EXIT WHEN. With either form, you can name the loop to be exited.

Syntax

exit_statement ::=

exit_statement
Description of the illustration exit_statement.gif

(boolean_expression ::=)

Keyword and Parameter Descriptions

boolean_expression

If and only if the value of this expression is TRUE, the current loop (or the loop labeled by label_name) is exited immediately.

EXIT

An unconditional EXIT statement (that is, one without a WHEN clause) exits the current loop immediately. Execution resumes with the statement following the loop.

label_name

Identifies the loop exit from: either the current loop, or any enclosing labeled loop.

Usage Notes

An EXIT statement can appear anywhere inside a loop, but not outside a loop. PL/SQL lets you code an infinite loop. For example, the following loop will never terminate normally so you must use an EXIT statement to exit the loop.

WHILE TRUE LOOP ... END LOOP;

If you use an EXIT statement to exit a cursor FOR loop prematurely, the cursor is closed automatically. The cursor is also closed automatically if an exception is raised inside the loop.

Examples

Related Topics