Part of good programming practice is error handling:
- Was the record found?
- Does the field contain proper data?
- Is it possible to add a record?
You know the sort.
The same is true with embedded SQL. Let's review two simple error-handling techniques for your embedded SQL statements.
WHENEVER
The simplest way to monitor for SQL errors is to use the SQL WHENEVER statement:
Type indicates the type of status encountered by the SQL statement. It is one of the following:
- NOT FOUND--True when the SQL statement finds no data (for example, FETCHing past the end of a result table or no records found for delete)
- SQLWARNING--True when the SQL statement executes successfully, but with warnings
- SQLERROR--True when the SQL statement fails
Action tells SQL what to do in case of one of the errors:
- CONTINUE--Continue with the next statement in your program
- GOTO (or GO TO) label--Go to a point in the program indicated by "label." In RPG, that would be a label with a TAG; in COBOL, a paragraph or section name.
A WHENEVER statement affects all SQL statements that follow in your program's listing (regardless of program logic), up to a subsequent WHENEVER statement. For any of the three types you do not code a WHENEVER for, SQL assumes an implicit WHENEVER, using an action of CONTINUE.
SQLCODE and SQLSTATE
I don't like WHENEVER because it's too general and it uses outdated and vilified programming constructs--GOTO and TAG. Instead of WHENEVER, I usually use SQLCODE or SQLSTATE.
SQLCODE and SQLSTATE are two fields that you define in your program. SQL sets these fields after every SQL statement executes. They are set to codes indicating the status of the SQL statement.
There are two ways to define SQLCODE and SQLSTATE. The first is to define them individually in your program. SQLCODE is a two-byte integer field (that's 10I 0 for RPG programmers), and SQLSTATE is a five-character alphanumeric field. The second is to bring into your program the special, predefined SQL Communications Area (SQLCA) data structure. This structure contains SQLCODE, SQLSTATE, and a plethora of additional information. You include the SQLCA by issuing the SQL statement INCLUDE SQLCA.
Defining an SQLCA is mutually exclusive from individually defining SQLCODE and SQLSTATE. You can't do both.
If you're an RPG programmer, you don't have to do either; RPG will include SQLCA automatically for you.
SQLCODE
SQLCODE is the easier and more generalized of the two fields. The table below shows how to do simple error handling by checking the values of SQLCODE:
Codes Indicating SQL Statement Status | |
---|---|
Value of SQLCODE | Meaning |
0 | SQL statement executed successfully. |
100 | SQL statement executed successfully, but no data was found. |
Greater than 0 | SQL statement executed successfully, but there was a warning. |
Less than 0 | SQL statement failed to executed. |
The neat thing about SQLCODE is that it can be used to get a description of the error message. All of the SQLCODE codes are in a message file called QSQLMSG. The message ID is constructed from the absolute value of SQLCODE:
- If the absolute value of SQLCODE is four digits or fewer, the message ID is SQLnnnn, with nnnn containing leading zeros if necessary.
- For five-digit codes, ID is SQnnnnn.
For example, the message corresponding to the SQLCODE of –7008 is SQL7008. The following command will give you the text of SQLCODE -7008:
DSPMSGD SQL7008 QSQLMSG
(Another field in SQLCA, SQLERRM, contains the message's substitution data).
SQLSTATE
SQLSTATE returns a more specific status code. It consists of five characters. The first two characters comprise a code that defines the class of the status' condition.
- Class '00' indicates a successful execution.
- Class '01' indicates a successful execution with warnings.
- Class '02' indicates no data found.
- All other classes indicate failed executions.
The last three characters give the specific status value.
Each SQLSTATE corresponds with one or more SQLCODEs. For example, SQLSTATE '00000' corresponds with SQLCODE 0, and '02000' corresponds to 100.
SQLSTATE is a DB2 standard across multiple platforms and is probably the best way to monitor your embedded SQL for errors.
GET DIAGNOSTICS
There is a third way to monitor SQL error messages: the GET DIAGNOSTICS statement. However, this statement does much, much more beyond error monitoring, and it's the topic of a future TechTip.
Good Programming Practice
Using either SQLCODE and SQLSTATE, or WHENEVER, allows for simple SQL error monitoring and is good programming practice, right along with the other error monitoring you do.
Useful Links
• SQL Messages
• SQLSTATE Class Codes
• SQLSTATE Values
• SQL Message Finder
Doug Eckersley is the iSeries programmer with a premier homebuilder in Columbus. He has been programming on the iSeries for 10 years and has been in the business for 15. He is certified by IBM. He welcomes back his Blue Jackets and is looking forward to the upcoming season.
LATEST COMMENTS
MC Press Online