Name
WHENEVER -- specify the action to be taken when an SQL statement causes a specific class condition to be raised
Synopsis
WHENEVER { NOT FOUND | SQLERROR | SQLWARNING } action
Description
Define a behavior which is called on the special cases (Rows not found, SQL warnings or errors) in the result of SQL execution.
Parameters
See Section "D.7.1 Setting Callbacks" or a description of the parameters.
Examples
EXEC SQL WHENEVER NOT FOUND CONTINUE END-EXEC. EXEC SQL WHENEVER SQLWARNING SQLPRINT END-EXEC. EXEC SQL WHENEVER SQLWARNING DO "warn" END-EXEC. EXEC SQL WHENEVER SQLERROR sqlprint END-EXEC. EXEC SQL WHENEVER SQLERROR CALL "print2" END-EXEC. EXEC SQL WHENEVER SQLERROR DO handle_error USING "select" END-EXEC. EXEC SQL WHENEVER SQLERROR DO sqlnotice USING 0 1 END-EXEC. EXEC SQL WHENEVER SQLERROR DO "sqlprint" END-EXEC. EXEC SQL WHENEVER SQLERROR GOTO error_label END-EXEC. EXEC SQL WHENEVER SQLERROR STOP END-EXEC.
A typical application is the use of WHENEVER NOT FOUND GOTO to handle looping through result sets:
EXEC SQL CONNECT TO testdb AS con1 END-EXEC. EXEC SQL ALLOCATE DESCRIPTOR d END-EXEC. EXEC SQL DECLARE cur CURSOR FOR SELECT current_database(), 'hoge', 256 END-EXEC. EXEC SQL OPEN cur END-EXEC. * when end of result set reached, break out of while loop EXEC SQL WHENEVER NOT FOUND GOTO NOTFOUND END-EXEC. PERFORM NO LIMIT EXEC SQL FETCH NEXT FROM cur INTO SQL DESCRIPTOR d END-EXEC ... END-PERFORM. NOTFOUND. EXEC SQL CLOSE cur END-EXEC. EXEC SQL COMMIT END-EXEC. EXEC SQL DEALLOCATE DESCRIPTOR d END-EXEC. EXEC SQL DISCONNECT ALL END-EXEC.
Compatibility
WHENEVER is specified in the SQL standard, but most of the actions are PostgreSQL extensions.