PLS-00376 illegal EXIT statement it must appear inside a loop

PLS-00376 illegal EXIT statement it must appear inside a loop

Oracle SQL Error: PLS-00376: illegal EXIT/CONTINUE statement; it must appear inside a loop

Cause:

An EXIT statement was found outside of a loop construct. The EXIT statement is used to exit prematurely from a loop and so must always appear within a loop.

Solution:

Either remove the EXIT statement or place it inside a loop.

Example:

DECLARE
	CURSOR c1 IS SELECT * FROM books;
	rec books%ROWTYPE;
BEGIN
	OPEN c1;
	FETCH c1 INTO rec;
	EXIT WHEN c1%NOTFOUND;
	DBMS_OUTPUT.PUT_LINE(rec.NAME);
	CLOSE c1;
END;

Output:

ORA-06550: line 7, column 3:
PLS-00376: illegal EXIT/CONTINUE statement; it must appear inside a loop

Correct

DECLARE
	CURSOR c1 IS SELECT * FROM books;
	rec books%ROWTYPE;
BEGIN
	OPEN c1;
	LOOP
	FETCH c1 INTO rec;
	EXIT WHEN c1%NOTFOUND;
	DBMS_OUTPUT.PUT_LINE(rec.NAME);
	END LOOP;
	CLOSE c1;
END;

Output:

anonymous block completed