Check after each SQL statement
To check for an exception, you can include code to explicitly test the value of SQLSTATE (or SQLCODE).
Tip: Decide whether to use SQLSTATE (and
the diagnostics area) or SQLCODE (and the sqlca structure)
to determine exception values. Use the chosen exception-handling variables
consistently. If you mix these two variables unnecessarily, you create
code that is difficult to maintain. Keep in mind that SQLSTATE is
the more flexible and portable of these two options.
For example, if you want to use SQLSTATE to check
whether a CREATE DATABASE statement has executed as expected, you
can use the code that the following figure shows.
As an alternative, you can write an exception-handling function that processes any exception. Your program can then call this single exception-handling function after each SQL statement.
The sqlstate_exception() function,
which the following figure shows, is an example of an exception-handling
function that uses the SQLSTATE variable and the diagnostics
area to check for warnings, the NOT FOUND condition, and runtime errors.
It is called after each SQL statement.
The sqlstate_exception() function,
which Example of an exception-handling function
that uses SQLSTATE shows, handles
exceptions as follows:
- If the statement was successful, sqlstate_exception() returns zero.
- If a NOT FOUND condition occurs after a SELECT or a FETCH statement, sqlstate_exception() returns
a value of
2
. - If a warning or a runtime error occurs—that is, if the first
two bytes of SQLSTATE are
"01"
(warning) or are greater than"02"
(error)—the sqlstate_exception() function calls the disp_sqlstate_err() function to display exception information. (For the code of the disp_sqlstate_err() function, see Lines 32 - 80.) - If SQLSTATE indicates an error, the sqlstate_exception() function uses the exit() system call to exit the program. Without this call to exit(), execution would continue at the next SQL statement after the one that had generated the error.
To handle errors, the sqlstate_exception() function can alternatively omit the exit() call and allow execution to continue. In this case, the function must return the SQLSTATE or SQLCODE (for errors specific to HCL OneDB™) value so the calling program can determine what action to take for a runtime error.