How to Declare User-Defined Handlers
The following examples show how to declare a user-defined handler in ESQL/COBOL:
UNIX:
IDENTIFICATION DIVISION.
PROGRAM-ID. TEST-PROG.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING_STORAGE SECTION.
EXEC SQL INCLUDE SQLCA END-EXEC.
...
PROCEDURE DIVISION.
BEGIN.
EXEC SQL CONNECT dbname END-EXEC.
* Call "C" routine to set error handler on.
* ErrProg will be called whenever an error occurs.
CALL "ErrTrap".
...
...
* Suppress display of error number (don't call ErrProg) for next statement by
* turning error handler off.
EXEC SQL SET_SQL(ERRORHANDLER = 0) END-EXEC.
EXEC SQL .....
* Turn error handler back on. ErrProg will now be
* called again whenever an error occurs.
CALL "ErrTrap".
...
END PROGRAM TEST-PROG.
The following is an example of a user-defined error handler:
IDENTIFICATION DIVISION.
PROGRAM-ID. ErrProg.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL INCLUDE SQLCA END-EXEC.
EXEC SQL BEGIN DECLARE SECTION END_EXEC.
01 errnum PIC S9(9) USAGE DISPLAY.
EXEC SQL END DECLARE SECTON END-EXEC.
PROCEDURE DIVISION.
BEGIN.
EXEC SQL INQUIRE_SQL(:errnum = ERRORNO) END-EXEC.
DISPLAY "Errnum is " errnum.
END PROGRAM ErrProg.
The following example is an embedded SQL/C routine that declares ErrProg to the Ingres runtime system:
ErrTrap()
{
extern int ErrProg();
EXEC SQL SET_SQL (ERRORHANDLER = ErrProg);
}
VMS:
IDENTIFICATION DIVISION.
PROGRAM-ID. Test-prog.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL INCLUDE SQLCA END-EXEC.
01 error_func PIC S9(9) USAGE COMP VALUE EXTERNAL
ErrProg.
...
PROCEDURE DIVISION
BEGIN.
EXEC SQL CONNECT dbname END-EXEC.
EXEC SQL SET_SQL (ERRORHANDLER = error_func)
END-EXEC.
. . .
END PROGRAM Test-prog.
IDENTIFICATION DIVISION.
PROGRAM-ID. ErrProg.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
EXEC SQL INCLUDE SQLCA END-EXEC.
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 errnum PIC S9(9) USAGE DISPLAY.
EXEC SQL END DECLARE SECTION END-EXEC.
PROCEDURE DIVISION.
BEGIN.
EXEC SQL INQUIRE_SQL (:errnum = ERRORNO) END-EXEC.
DISPLAY "Errnum is " errnum.
END PROGRAM ErrProg.