8. OpenSQL Statements : Whenever : Examples: Whenever
 
Share this page                  
Examples: Whenever
1. During program development, print all errors and continue with next statement.
exec sql whenever sqlerror call sqlprint;
2. During database cursor manipulation, close the cursor when no more rows are retrieved.
exec sql open cursor1;
    exec sql whenever not found goto close_cursor;

    loop until whenever not found is true
        exec sql fetch cursor1
            into :var1, :var2;
        print and process the results;
    end loop;

    close_cursor:
        exec sql whenever not found continue;
        exec sql close cursor1;
3. Stop program upon detecting an error or warning condition.
exec sql whenever sqlerror stop;
    exec sql whenever sqlwarning stop;
4. Reset WHENEVER actions to default within an error handling block.
error_handle:
        exec sql whenever sqlerror continue;
        exec sql whenever sqlwarning continue;
        exec sql whenever not found continue;
    ...
    handle cleanup;
    ...
5. Always confirm that the connect statement succeeded before continuing.
exec sql whenever sqlerror stop;
    exec sql connect :dbname;
    exec sql whenever sqlerror continue;