SQLERRMESSAGE

If an SQL error occurs, the SQLERRMESSAGE operator returns the error message associated with the error code. This is a character string that can be displayed to the user with the ERROR instruction.

ERROR SQLERRMESSAGE 
Changes to function fetch_cust (custquery.4gl)
01 FUNCTION fetch_cust (p_fetch_flag)
02    DEFINE p_fetch_flag SMALLINT,
03              fetch_ok SMALLINT
04
05   LET fetch_ok = FALSE
06   IF (p_fetch_flag = 1) THEN
07      WHENEVER ERROR CONTINUE
08      FETCH NEXT cust_curs 
09         INTO mr_custrec.*
10      WHENEVER ERROR STOP
11   ELSE
12       WHENEVER ERROR CONTINUE
13       FETCH PREVIOUS cust_curs 
14         INTO mr_custrec.*
15       WHENEVER ERROR STOP
16   END IF
17
18   CASE
19   WHEN (SQLCA.SQLCODE = 0)
20     LET fetch_ok = TRUE
21   WHEN (SQLCA.SQLCODE = NOTFOUND)
22     LET fetch_ok = FALSE
23   WHEN (SQLCA.SQLCODE < 0)
24     LET fetch_ok = FALSE
25     ERROR SQLERRMESSAGE
26   END CASE 
27
28  RETURN fetch_ok 
29
30 END FUNCTION
Note:
  • Lines 08, 09, 13,14 The SQL statements are surrounded by WHENEVER ERROR statements. If an error occurs during the SQL statements, the program will continue. The error handling is reset to the default (STOP) immediately after each SQL statement so that failures of other program statements will not be ignored.
  • Lines 18 to 26 Immediately after the WHENEVER ERROR STOP statement, the SQLCA.SQLCODE is checked, to see whether the SQL statement succeeded. A CASE statement is used, since there are more than two conditions to be checked.