Mimer SQL Documentation TOC PREV NEXT INDEX

Mimer SQL Developer Site


Handling errors and exceptions


Errors may arise at three general levels in an SQL module. These are syntax, semantic and run-time errors.

Syntax Errors

Syntax errors are constructions that break the rules for formulating SQL statements. For example:

The preprocessor does not accept syntactically incorrect statements. The error must be corrected before the program can be successfully preprocessed.

Semantic Errors

Semantic errors arise when SQL statements are formulated in full accordance with the syntax rules, but do not reflect the programmer's intentions correctly. Semantic errors are not detected by the MSQL preprocessor.

Run-time Errors

Run-time errors and exception conditions (for example warnings) arising during execution of SQL module procedures are signaled in the same was as in ESQL, see Handling Errors and Exceptions.

The errors cannot be caught with WHENEVER statements that are used in ESQL, the host application has to rely on the values of SQLCODE and SQLSTATE.

Examples

Example SQL module:
 PROCEDURE connect_sysadm 
     SQLCODE;
 CONNECT TO '' USER 'SYSADM' USING 'SYSADM';
 
 PROCEDURE get_diagn
     :errcode INT
     :errmsg  VARCHAR(300)
     SQLCODE;
 GET DIAGNOSTICS EXCEPTION 1
     :errcode = native_error,
     :errmsg  = message_text;
Example host application in C:
 void connect_sysadm(int* sqlcode);
 void get_diagn(int* errcode, char errmsg[301], int* sqlcode);
 
 int main()
 {
     int sqlcode;
     int errcode;
     char errmsg[301];
 
     connect_sysadm(&sqlcode);
     if (sqlcode != 0)
     {
         printf("Failed to connect SYSADM.\n");
 
         get_diagn(&errcode, errmsg, &sqlcode);
         if (sqlcode != 0)
         {
             printf("Failed to get diagnostics message.\n");
         }
         else
         {
             printf("errcode: %d\n", errcode);
             printf("errmsg: %s\n", errmsg);
         }
     }
Example host application in Fortran:
       INTEGER*4 SQLCODE
       INTEGER*4 ERRCODE
       CHARACTER*300 ERRMSG
 
       CALL CONNECT_SYSADM(SQLCODE)
       IF (SQLCODE .NE. 0) THEN
           PRINT *, 'Failed to connect SYSADM.'
 
           IF (SQLCODE .NE. 0) THEN
               PRINT *, 'Failed to get diagnostics message.'
           ELSE
               PRINT *, 'errcode: ', ERRCODE
               WRITE(*, '(X,72A)') 'errmsg: ', ERRMSG
           END IF
       END IF
Example host application in Cobol:
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 SQLCODE PIC S9(9) USAGE IS BINARY.
        01 ERRCODE PIC S9(9) USAGE IS BINARY.
        01 ERRMSG PICTURE X(300).
 
        PROCEDURE DIVISION.
        HEAD SECTION.
 
        MAIN.
 
            CALL "CONNECT_SYSADM" USING SQLCODE.
            IF SQLCODE IS NOT ZERO
                DISPLAY "Failed to connect SYSADM."
 
                CALL "GET_DIAGN" USING ERRCODE, ERRMSG, SQLCODE.
                IF SQLCODE IS NOT ZERO
                    DISPLAY "Failed to get diagnostics message."
                ELSE
                    DISPLAY "errcode: " ERRCODE
                    DISPLAY "errmsg: " ERRMSG
                END-IF.
            END-IF.
Example host application in Pascal:
 type message = packed array [1..300] of char;
 
 var
 sqlcode : integer;
 errcode : integer;
 errmsg : message;
 
 procedure connect_sysadm(var sqlcode : integer); external;
 procedure get_diagn(var errcode : integer, var errmsg : message,
    var sqlcode : integer); external;
 
 begin
    connect_sysadm(sqlcode);
    if sqlcode <> 0 then
    begin
       writeln('Failed to connect SYSADM.');
 
       get_diagn(errcode, errmsg, sqlcode);
       if sqlcode <> 0 then
       begin
          writeln('Failed to get diagnostics message.');
       end;
       else
       begin
          writeln('errcode: ', errcode)
          writeln('errmsg: ', errmsg);
       end;

Mimer
Mimer Information Technology AB
Phone: +46 18 780 92 00
info@mimer.com
Mimer SQL Documentation TOC PREV NEXT INDEX