EXIT is a control-flow statement available in procedural SQL dialects such as PostgreSQL’s PL/pgSQL and Oracle’s PL/SQL. When executed, it stops further iteration of the innermost loop (WHILE, FOR, LOOP) or the explicitly named block and continues execution with the first statement following that loop or block. If a WHEN clause is supplied, EXIT executes only when the Boolean condition evaluates to TRUE, enabling early termination based on runtime logic. Using labels lets developers exit outer loops without using auxiliary flags or complex logic.EXIT does not commit or roll back transactions and should not be confused with client-side commands like QUIT or with RETURN, which leaves a function entirely. It is evaluated at runtime, so unreachable EXIT statements are syntactically allowed but never executed. Misusing EXIT in plain SQL (outside a procedural block) raises a syntax error.
LOOP, WHILE, FOR, CONTINUE, RETURN, LEAVE (MySQL), GOTO (T-SQL)
PostgreSQL 7.0 (PL/pgSQL)
EXIT leaves the current loop or labeled block and continues executing the remaining code in the function. RETURN exits the entire function or procedure immediately and optionally supplies a return value.
Yes. Provide a label on the outer loop or block and reference that label in the EXIT statement (e.g., EXIT outer_loop;).
No. EXIT only affects control flow inside a procedural block. Transaction control must be handled separately with COMMIT or ROLLBACK.
Not under that name. MySQL uses LEAVE to accomplish the same task inside stored programs.