appear below the point where the handler is set, unless a
different action was set for the same condition between the first
<literal>EXEC SQL WHENEVER</literal> and the SQL statement causing
the condition, regardless of the flow of control in the C program.
So neither of the two following C program excerpts will have the
desired effect:
<programlisting>
/*
* WRONG
*/
int main(int argc, char *argv[])
{
...
if (verbose) {
EXEC SQL WHENEVER SQLWARNING SQLPRINT;
}
...
EXEC SQL SELECT ...;
...
}
</programlisting>
<programlisting>
/*
* WRONG
*/
int main(int argc, char *argv[])
{
...
set_error_handler();
...
EXEC SQL SELECT ...;
...
}
static void set_error_handler(void)
{
EXEC SQL WHENEVER SQLERROR STOP;
}
</programlisting>
</para>
</sect2>
<sect2 id="ecpg-sqlca">
<title>sqlca</title>
<para>
For more powerful error handling, the embedded SQL interface
provides a global variable with the name <varname>sqlca</varname>
(SQL communication area)
that has the following structure:
<programlisting>
struct
{
char sqlcaid[8];
long sqlabc;
long sqlcode;
struct
{
int sqlerrml;
char sqlerrmc[SQLERRMC_LEN];
} sqlerrm;
char sqlerrp[8];
long sqlerrd[6];
char sqlwarn[8];
char sqlstate[5];
} sqlca;
</programlisting>
(In a multithreaded program, every thread automatically gets its
own copy of <varname>sqlca</varname>. This works similarly to the
handling of the standard C global variable
<varname>errno</varname>.)
</para>
<para>
<varname>sqlca</varname> covers both warnings and errors. If
multiple warnings or errors occur during the execution of a
statement, then <varname>sqlca</varname> will only contain
information about the last one.
</para>
<para>
If no error occurred in the last <acronym>SQL</acronym> statement,
<literal>sqlca.sqlcode</literal> will be 0 and
<literal>sqlca.sqlstate</literal> will be
<literal>"00000"</literal>. If a warning or error occurred, then
<literal>sqlca.sqlcode</literal> will be negative and
<literal>sqlca.sqlstate</literal> will be different from
<literal>"00000"</literal>. A positive
<literal>sqlca.sqlcode</literal> indicates a harmless condition,
such as that the last query returned zero rows.
<literal>sqlcode</literal> and <literal>sqlstate</literal> are two
different error code schemes; details appear below.
</para>
<para>
If the last SQL statement was successful, then
<literal>sqlca.sqlerrd[1]</literal> contains the OID of the
processed row, if applicable, and
<literal>sqlca.sqlerrd[2]</literal> contains the number of
processed or returned rows, if applicable to the command.
</para>
<para>
In case of an error or warning,
<literal>sqlca.sqlerrm.sqlerrmc</literal> will contain a string
that describes the error. The field
<literal>sqlca.sqlerrm.sqlerrml</literal> contains the length of
the error message that is stored in
<literal>sqlca.sqlerrm.sqlerrmc</literal> (the result of
<function>strlen()</function>, not really interesting for a C
programmer). Note that some messages are too long to fit in the
fixed-size <literal>sqlerrmc</literal> array; they will be truncated.
</para>
<para>
In case of a warning, <literal>sqlca.sqlwarn[2]</literal> is set
to <literal>W</literal>. (In all other cases, it is set to
something different from <literal>W</literal>.) If
<literal>sqlca.sqlwarn[1]</literal> is set to
<literal>W</literal>, then a value was truncated when it was
stored in a host variable. <literal>sqlca.sqlwarn[0]</literal> is
set to <literal>W</literal> if any of the other elements are set
to indicate a warning.
</para>
<para>
The fields <structfield>sqlcaid</structfield>,
<structfield>sqlabc</structfield>,
<structfield>sqlerrp</structfield>,