PL/SQL EXIT Statement
PL/SQL EXIT Statement (Sequential Control Statement) are control your iteration loop, PL/SQL EXIT statement to exit the loop.
- EXIT Statement : This statement to exit the loop.
- EXIT WHEN Statement : This statement to exit, when WHEN clauses condition true.
EXIT Statement
EXIT statement unconditionally exit the current loop iteration and transfer control to end of current loop. EXIT statement writing syntax,
Syntax
[ label_name ] LOOP
statement(s);
EXIT;
END LOOP [ label_name ];
Example
DECLARE
no NUMBER := 5;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE ('Inside value: no = ' || no);
no := no -1;
IF no = 0 THEN
EXIT;
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Outside loop end'); -- After EXIT control transfer this statement
END;
/
Result
Inside value: no = 5
Inside value: no = 4
Inside value: no = 3
Inside value: no = 2
Inside value: no = 1
Outside loop end
PL/SQL procedure successfully completed.
Inside value: no = 4
Inside value: no = 3
Inside value: no = 2
Inside value: no = 1
Outside loop end
PL/SQL procedure successfully completed.
EXIT WHEN Statement
EXIT WHEN statement unconditionally exit the current loop iteration when WHEN clause condition true. EXIT WHEN statement writing syntax,
Syntax
[ label_name ] LOOP
statement(s);
EXIT WHEN condition;
END LOOP [ label_name ];
Example
SQL>DECLARE
i number;
BEGIN
LOOP
dbms_output.put_line('Hello');
i:=i+1;
EXIT WHEN i>5;
END LOOP;
END;
/
Result
Hello
Hello
Hello
Hello
PL/SQL procedure successfully completed.
Hello
Hello
Hello
PL/SQL procedure successfully completed.