When used with an application program, the error-checking utility files provide helpful error information, and make debugging a DB2 program much easier. Most of the error-checking utilities use the DB2 APIs GET SQLSTATE MESSAGE (sqlogstt) and GETERROR MESSAGE (sqlaintp) to obtain pertinent SQLSTATE and SQLCA information related to problems encountered in program execution. The CLI utility file, utilcli.c, does not use these DB2 APIs; instead it uses equivalent CLI statements. With all the error-checking utilities, descriptive error messages are printed out to allow the developer to quickly understand the problem. Some DB2 programs, such as routines (stored procedures and user-defined functions), do not need to use the utilities.
| Language | Non-embedded SQL source file | Non-embedded SQL header file | Embedded SQL source file | Embedded SQL header file |
|---|---|---|---|---|
| C |
utilapi.c | utilapi.h | utilemb.sqc | utilemb.h |
| C++ |
utilapi.C | utilapi.h | utilemb.sqC | utilemb.h |
| IBM® COBOL |
checkerr.cbl | n/a | n/a | n/a |
| Micro Focus COBOL |
checkerr.cbl | n/a | n/a | n/a |
In order to use the utility functions, the utility file must first be compiled, and then its object file linked in during the creation of the target program's executable file. Both the makefile and build files in the samples directories do this for the programs that require the error-checking utilities.
/* macro for embedded SQL checking */
#define EMB_SQL_CHECK(MSG_STR) \
SqlInfoPrint(MSG_STR, &sqlca, __LINE__, __FILE__); \
if (sqlca.sqlcode < 0) \
{ \
TransRollback(); \
return 1; \
}
EXEC SQL DELETE FROM org
WHERE deptnumb = :hostVar1 AND
division = :hostVar2;
EMB_SQL_CHECK("Delete with host variables -- Execute");
The EMB_SQL_CHECK macro
ensures that if the DELETE statement fails, the transaction
will be safely rolled back, and an appropriate error message printed
out.Developers are encouraged to use and expand upon these error-checking utilities when creating their own DB2 programs.