There are a number of errors that may occur at runtime if you do not set up the ODBC environment for a NetCOBOL SQL application properly.
For Example, if you code an erroneous SQL statement, within an EXEC SQL …. ENDEXEC statement, the NetCOBOL compiler may catch the error and issue an error message.
In some cases, however, the error will not be determined until runtime. Some of the most common examples are listed below.
A common runtime error comes from not specifying to the Runtime Environment the name of a valid .inf file. For example, in the above example application, if you forget to specify the line:
@ODBC_Inf=c:\mysql\mysql.inf
In COBOL85.CBR, you will receive an error dialog box as shown in Figure. This error occurs whenever no @ODBC_inf parameter is specified.
If you specify an @ODBC_inf parameter and it is found, but it contains the name of a file that does not exist or cannot be found, you receive the error dialog box shown in Figure.
If your COBOL program attempts to connect to a server that is not specified in your .inf file, you will receive the error dialog shown in Figure.
Capturing Other Errors
There are other errors that will not produce an Error Message dialog box. In fact, they will only produce errors by returning non-zero values in SQLSTATE and/or SQLCODE. You may notice that the simple application above only checks SQLSTATE. You will find two additional data items useful if you add them to your EXEC SQL BEGIN DECALRE SECTION END-EXEC group. They are SQLCODE and SQLMSG. They are defined as follows:
EXEC SQL BEGIN DECLARE SECTION END-EXEC. 01 EmpID Pic X(9) Value Spaces. 01 FName Pic X(20) Value Spaces. 01 LName Pic X(30) Value Spaces. 01 SQLSTATE Pic X(5) Value Spaces. 01 SQLCODE Pic S9(9) Comp-5 Value Zeroes. 01 SQLMSG Pic X(254) Value Spaces. EXEC SQL END DECLARE SECTION END-EXEC.
SQLMSG is of particular use. The ODBC driver will place descriptive error messages in this variable. For example, if you code the name of an ODBC data source that does not exist in the “@SQL_DATASRC=” parameter in your .inf file, you will not receive an error dialog box. Instead you will receive a value of IM002 in SQLSTATE. This is not very helpful. But if you look at the value contained in SQLMSG, it will be:
[Microsoft] [ODBC Driver Manager] Data source name not found and no default driver specified.
There are some SQL syntax errors that will not be caught by the NetCOBOL compiler and will only show up at runtime. For example, if you code an erroneous Where clause in a Select statement such as:
EXEC SQL DECLARE CUR1 CURSOR FOR SELECT emp_id, fname, lname FROM employee Where emp_id Not NULL END-EXEC.
Note that “Not NULL” is invalid SQL Code. When this statement is executed at runtime, you will receive a value of 37000 in SQLSTATE (again, not very helpful), but SQLMSG will contain:
[Microsoft] [ODBC SQL Server Driver] [SQL Server] Incorrect syntax near the keyword ‘Null’.
It is thus a good idea to always include SQLMSG in your COBOL program and to interrogate it whenever error messages occur. You may wish to display SQLMSG when debugging after key SQL statements in your program.