WHENEVER is an error-handling directive, not a data-manipulation statement. In Oracle SQL*Plus (and compatible tools such as SQLcl) it instructs the client to automatically EXIT or CONTINUE when an SQLERROR (database error) or OSERROR (operating-system error) occurs while a script is running. EXIT can optionally commit or roll back the current transaction and return a status code to the calling shell.In embedded SQL for host languages (Pro*C, IBM DB2, Informix ESQL/C, MySQL C API, PostgreSQL ECPG) WHENEVER is compiled into the application. It traps runtime conditions (SQLERROR, SQLWARNING, NOT FOUND) and triggers an action such as GOTO label, DO procedure, or STOP. This lets developers centralize error handling without wrapping every statement in explicit checks.Key points:- WHENEVER sets a stateful directive that remains in force until redefined.- Multiple conditions can be set separately (e.g., one rule for SQLERROR and another for NOT FOUND).- In SQL*Plus, EXIT ends the client session, returning the specified value to the operating system. CONTINUE resumes script execution.- Transaction outcome is governed by optional COMMIT or ROLLBACK keywords.- The directive is evaluated after each SQL or host command; it does not change database semantics, only client behavior.- WHENEVER is client-side; it does not exist inside PL/SQL or server-side stored procedures.
condition
(enum) - SQLERROR, OSERROR (SQL*Plus) or SQLERROR, SQLWARNING, NOT FOUND (embedded SQL)action
(enum) - EXIT or CONTINUE (SQL*Plus) or GOTO label, CALL procedure, STOP, CONTINUE (embedded)status_code
(int) - Numeric code returned to host shell when EXIT is used (SQL*Plus only)commit_flag
(enum) - COMMIT or ROLLBACK transaction when EXIT/CONTINUE fires (SQL*Plus only)EXIT, ROLLBACK, COMMIT, DECLARE HANDLER, EXCEPTION handling, SQLWARNING, NOT FOUND
Oracle SQL*Plus 1.0 (1980); ANSI Embedded SQL-86 for host languages
It defines an automatic EXIT or CONTINUE action when a specified error condition occurs during script or embedded SQL execution.
Place `WHENEVER SQLERROR EXIT 1 ROLLBACK;` at the top. The script stops on the first SQL error, rolls back, and returns exit code 1 to the shell.
No. WHENEVER is interpreted by the client or embedded SQL pre-compiler, not by the PL/SQL engine. Use EXCEPTION blocks inside PL/SQL instead.
The concept exists in most embedded SQL implementations, but the syntax outside Oracle SQL*Plus may vary. Always check your client documentation before porting.