Was this helpful?
Error Handling Using the SQLCA
User-defined error, message and dbevent handlers offer the most flexibility for handling errors, database procedure messages, and database events. For more information, see Advanced Processing.
However, you can do error handling with the SQLCA implicitly by using whenever statements, or explicitly by checking the contents of the SQLCA fields sqlcode, sqlerrd, and sqlwarn0.
Error Handling with the Whenever Statement
The syntax of the whenever statement is:
exec sql whenever condition action;
condition
Specifies the error condition. Valid error conditions are dbevent, sqlwarning, sqlerror, sqlmessage, and not found.
action
Specifies the action to be taken. Valid actions are continue, stop, goto a label, and call a C procedure.
For a detailed description of this statement, see the SQL Reference Guide.
In C, all labels and procedure names must be legal C identifiers, beginning with an alphabetic character or an underscore. If the label is an embedded SQL reserved word, specify it in quotes. The label targeted by the goto action must be in the scope of all subsequent embedded SQL statements until another whenever statement is encountered for the same action. This is necessary because the preprocessor may generate the C statement:
if (condition) goto label;
after an embedded SQL statement. If the scope of the label is invalid, the C compiler generates an error.
The same scope rules apply to procedure names used with the call action. The reserved procedure sqlprint, which prints errors or database procedure messages and then continues, is always in the scope of the program. When a whenever statement specifies a call as the action, the target procedure is called, and after its execution, control returns to the statement following the statement that caused the procedure to be called. Consequently, after handling the whenever condition in the called procedure, you may want to take some action, instead of merely issuing a C return statement. The C return statement causes the program to continue execution with the statement following the embedded SQL statement that generated the error.
You can also use user-defined handlers for error handling. For more information, see the SQL Reference Guide.
The following example demonstrates use of the whenever statement in the context of printing some values from the Employee table. The comments do not relate to the program but to the use of error handling.
Example: whenever statement usage
exec sql include sqlca;
Db_Test()
{
    exec sql begin declare section;
         short eno;
         char ename[21];
         char age;
    exec sql end declare section;
       exec sql declare empcsr cursor for
         select eno, ename, age
         from employee;
    /*
    ** An error when opening the personnel database will
    ** cause the error to be printed and the program
    ** to abort.
    */
    exec sql whenever sqlerror stop;
    exec sql connect personnel;
    /* Errors from here on will cause the program to
    ** clean up
    */
    exec sql whenever sqlerror call Clean_Up;
    exec sql open empcsr;
    printf("Some values from the \"employee\" table.\n");
    /*
    ** When no more rows are fetched, close the cursor
    */
    exec sql whenever not found goto close_csr;
    /*
    ** The last executable embedded SQL statement was an
    ** OPEN, so we know that the value of "sqlcode"
    ** cannot be SQLERROR or NOT FOUND.
    */
    while (1) /* Loop is broken by NOT FOUND */
    {
        exec sql fetch empcsr
            into :eno, :ename, :age;
            /*
            ** This "printf" does not execute after the
            ** previous FETCH returns the NOT FOUND
            ** condition.
            */
            printf("%d, %s, %d\n", eno, ename, age);
    }
    /*
    ** From this point in the file onwards, ignore all
    ** errors. Also turn off the NOT FOUND condition,
    ** for consistency
    */
    exec sql whenever sqlerror continue;
    exec sql whenever not found continue;
 Close_Csr:
    exec sql close empcsr;
    exec sql disconnect;
 }
/*
** Clean_Up: Error handling procedure (print error and disconnect).
*/
Clean_Up()
{
    exec sql begin declare section;
        char errmsg[101];
    exec sql end declare section;
    exec sql inquire_sql (:errmsg = ERRORTEXT);
    printf("Aborting because of error:\n%s\n", errmsg);
    exec sql disconnect;
    exit(-1); /* Do not return to Db_Test */
}
Whenever Goto Action In Embedded SQL Blocks
An embedded SQL block-structured statement is delimited by the words begin and end. For example, the select loop and unloadtable loops are all block-structured statements. You can only terminate these statements by the methods specified for the particular statement in the SQL Reference Guide. For example, the select loop is terminated either when all the rows in the database result table are processed or by an endselect statement. The unloadtable loop is terminated either when all the rows in the forms table field are processed or by an endloop statement.
Therefore, if you use a whenever statement with the goto action in an SQL block, you must avoid going to a label outside the block. Such a goto causes the block to be terminated without issuing the runtime calls necessary to clean up the information that controls the loop. (For the same reason, you must not issue a C return or goto statement that causes control to leave or enter the middle of an SQL block.) The target label of the whenever goto statement should be a label in the block. However, if it is a label for a block of code that cleanly exits the program, the above precaution need not be taken.
The above information does not apply to error handling for database statements issued outside an SQL block, or to explicit hard-coded error handling. See the example of hard-coded error handling in The Table Editor Table Field Application.
Explicit Error Handling
Programs can also handle errors by inspecting values in the SQLCA structure at various points. For further details, see the SQL Reference Guide.
The following example is functionally the same as the previous example, except that the error handling is hard-coded in C statements.
Example: Explicit error handling
exec sql include sqlca;
# define NOT_FOUND 100
Db_Test()
{
    exec sql begin declare section;
        short eno;
        char ename[21];
        char age;
    exec sql end declare section;
    exec sql declare empcsr cursor for
        select eno, ename, age
        from employee;
    /* Exit if database cannot be opened */
    exec sql connect personnel;
    if (sqlca.sqlcode < 0)
    {
        printf("Cannot access database.\n");
        exit(-1);
    }
/* Error if cannot open cursor */
exec sql open empcsr;
 if (sqlca.sqlcode < 0)
    Clean_Up("OPEN \"empcsr\"");
printf("Some values from the \"employee\"
    table.\n");
/*
** The last executable embedded SQL statement was an OPEN, so we know
** that the value of "sqlcode" cannot be SQLERROR or NOT FOUND.
*/
while (sqlca.sqlcode == 0)
/* Loop broken by NOT FOUND */
{
        exec sql fetch empcsr
            into :eno, :ename, :age;
        if (sqlca.sqlcode < 0)
             Clean_Up("FETCH <"empcsr\"");
        /* Do not print the last values twice */
        else if (sqlca.sqlcode != NOT_FOUND)
        printf("%d, %s, %d\n", eno, ename, age);
    }
    exec sql close empcsr;
    exec sql disconnect;
}
/*
** Clean_Up: Error handling procedure
*/
Clean_Up(stmt)
 char *stmt;
 {
    exec sql begin declare section;
        char *err_stmt = stmt;
        char errmsg[101];
    exec sql end declare section;
    exec sql inquire_sql (:errmsg = ERRORTEXT);
    printf("Aborting because of error in %s:\n%s\n",
        err_stmt, errmsg);
    exec sql disconnect;
    exit(-1); /* Do not return to Db_Test */
}
How to Determine the Number of Affected Rows
The third element of the SQLCA array sqlerrd indicates how many rows were affected by the last row-affecting statement. This element is referenced by sqlerrd[2] rather than sqlerrd[3] as in other languages, because C subscripts begin at number 0.
The following program fragment, which deletes all employees whose employee numbers are greater than a given number, demonstrates how to use sqlerrd.
Example: sqlerrd usage
exec sql include sqlca;
 Delete_Rows(lower_bound)
 int lower_bound;
 {
    exec sql begin declare section;
        int lower_bound_num = lower_bound;
    exec sql end declare section;
    exec sql delete from employee
        where eno > :lower_bound_num;
    /* Print the number of employees deleted */
    printf("%d row(s) were deleted.\n",
     sqlca.sqlerrd[2]);
    }
Last modified date: 11/28/2023