You can use WHENEVER statements to program standard error handling procedures for errors and exceptions that can occur when embedded SQL statements are executed. You can check four different types of database system messages, and choose from four standard reactions to these messages.
Use a WHENEVER statement if, for example, the application program does not query the values of the messages sqlcodeor sqlwarn0 after an SQL statement.
EXEC SQL WHENEVER <condition> <action>;
Where <condition> is one of the following cases:
SQLWARNING |
sqlwarn0 has
the value
W. |
SQLERROR |
sqlcode has a
negative value. |
NOT FOUND |
sqlcode has the value 100 (ROW NOT FOUND). No table row found. |
SQLEXCEPTION |
sqlcode has a positive value greater than 100. An exception has occurred. |
For <action>, specify one of the possible actions for WHENEVER statements.
If SQLERROR occurs, the application program must terminate, and the error must be analyzed.
If you call an error handling function <action> that also contains SQL statements, and these SQL statements can send error messages, then start the function with a WHENEVER CONTINUE statement to avoid endless loops.
Example
EXEC SQL WHENEVER SQLEND CALL CheckSQLCode(&sqlca);
EXEC SQL SELECT DIRECT
NAME, LOC
INTO :name, :loc FROM HOTEL KEY HNO = :hno;
void
CheckSQLCode(sqlcatype *sqlca)
{
if (sqlca->sqlcode != 0)
{
code =
sqlca->sqlcode;
EXEC SQL WHENEVER
SQLERROR CONTINUE;
EXEC SQL INSERT INTO
ERROR VALUES (:code);
}
}