You are on page 1of 4

The standards and guidelines specific to COBOL SQL.

1. The SQLCODE must be checked after every SQL statement.


The Declare cursor statement is only a declarative, and it gets no return code from DB2.
All other SQL calls get some return code. Return code data from the DB2 database
system gets automatically loaded in the SQLCA communications area.
2. Every program must include the SQLCA and a DCLGEN for each table being coded against.
The DCLGEN is predefined with host variables that match the column definitions. They
are used to select data into, insert and update from, and serve as the host variables in
any Where clause.
If DCLGEN fields are not being used, then any program declaring variables in the code
must make sure that the variable being declared exactly matches the definition in DB2. If
it doesn’t, then there is a possibility that DB2 may not choose an index to process.
For example, if Column1 is defined as an Integer, then the host variable in COBOL should
be defined as S9(9) comp.
3. Every program must have a consistent DB2 ABEND routine.
For batch programs, it is easiest to have a called program that handles the display of the
SQLCA fields and calls the DSNTIAR DB2 routine to display further DB2 messages. For
online programs, sometimes it is good to write out the SQLCA and DSNTIAR.
The SQLCA contains a lot of information specific to a call that is critical to
troubleshooting an error. It is important to write out all the information captured. Make
sure that at least the SQLSTATE is displayed, along with the SQLCODE.
01  ERROR­MESSAGE.
        02  ERROR­LEN   PIC S9(4)  COMP VALUE +720.
        02  ERROR­TEXT  PIC X(72)  OCCURS 10 TIMES
                                   INDEXED BY ERROR­INDEX.
77  ERROR­TEXT­LEN      PIC S9(9)  COMP VALUE +72.

CALL 'DSNTIAR' USING SQLCA ERROR­MESSAGE ERROR­TEXT­LEN.

DSNT408I SQLCODE = ­501, ERROR:  THE CURSOR IDENTIFIED IN A FETCH OR
         CLOSE STATEMENT IS NOT OPEN
DSNT418I SQLSTATE   = 24501 SQLSTATE RETURN CODE
DSNT415I SQLERRP    = DSNXERT SQL PROCEDURE DETECTING ERROR
DSNT416I SQLERRD    = ­315  0  0  ­1  0  0 SQL DIAGNOSTIC INFORMATION
DSNT416I SQLERRD    = X'FFFFFEC5'  X'00000000'  X'00000000'
         X'FFFFFFFF'  X'00000000'  X'00000000' SQL DIAGNOSTIC INFORMATION
4. Never code Select * in a program.
Only code for the columns needed. If a program needs all the columns, then code each
one. This will prevent an ABEND if a new column is ever added to the table. The fewer
columns being brought into the program, the more efficient the processing.
More columns can have an effect on performance due to larger sort sizes, possible
index-only processing, and join types. When DB2 looks at which join type is best, part of
its analysis is the number of columns from each table being selected.
5. Make sure any columns defined as NULLABLE contain a null indicator host variable as part of
the Select, Insert, or Update statements.
This is most important in Select statements because DB2 will return an invalid -305
SQLCODE when it returns a column of null to the program and there is no null indicator
specified. These null indicators must be defined in working storage as Pic S9 (4) Comp.
It is preferable to code the VALUE, COALESCE, or IFNULL SQL scalar function for any
NULLABLE columns because the program will not receive null indicators from DB2. This
will alleviate -305 SQL errors where a program is not set up to handle the null indicator.
It will also spare the program from having to define the null indicators in working
storage.
For example, Select COALESCE (PK_ID, 0) will return the PK_ID value if there is one, or it
will return a zero if it is null. This could also be coded with the VALUE and IFNULL
functions.
All three would return the same result. The default specified must match the column
definition. For example, since PK_ID is numeric, then the default must be a numeric—in
this case, zero.
6. Any SQL statement that contains one of the following aggregate functions should have a Null-
Indicator host variable as part of the select (MIN MAX, AVG and SUM).
DB2 will return a null indicator to the program if it finds no data to process these
functions, and the COBOL program will have to define a null indicator.
If the program is not set up with a null indicator, an invalid -305 SQLCODE is returned. It
is preferable to code the VALUE, COALESCE, or IFNULL function to alleviate any null
indicator logic.
For example: SELECT IFNULL (AVG (SALARY), 0) FROM EMP WHERE WORKDEPT = 'XYZ'
This will either return the average if rows are found or a zero if no rows were met in
order to calculate an average.
7. Minimize the number of times cursors are opened and closed during execution.
If most of the time the open cursor and fetch retrieves only one row, then code a simple
Select statement and execute the cursor processing only when a -811 (duplicate rows)
SQLCODE is returned.
Do not break up processing into multiple cursors unless performance seems to be an
issue.
If it takes a seven-table join, then code all seven tables in one cursor and let DB2 do the
work. When you break it up, the process usually takes longer due to the extra times DB2
is sent SQL statements to process. So break up the join only when all other tuning efforts
have been applied. Typically it would be more efficient to execute a seven-table join.
8. CASE expressions should always contain an ELSE clause.
If none of the conditions in the CASE are met, then DB2 will return a null (via a null
indicator) to the program.
If the program is not set up to handle a null being returned from the CASE expression,
then a -305 SQLCODE is returned, which usually causes the program to ABEND.
9. Always display counts for the number of Selects, Inserts, Updates, Deletes, and Open cursors
that have been executed in the program.
The overhead in COBOL to define the counters and increment them through the
processing is minimal to the overall runtime of the program.
Displaying these counts provides invaluable information when problems occur, helping a
developer figure out which program to look into. Make sure the counts are displayed on
every ABEND and at the end of processing.
10. Always display the values in host variables for a SQL statement that has an invalid SQL return
code and the program goes into its ABEND error routine.
11. Watch out for any SQL warnings that may occur in an SQL statement. Most programs seem
to ignore warnings that many times help to detect potential problems.
There are two indications of a warning message in the SQLCA:
One is a positive SQLCODE other than +100; the other is a W in the SQLCA’s SQLWARN0
field.
When either of these exists, DB2 is issuing a warning that something worrisome
happened on the prior call and that while you may have received data back, it may not
be what you expected.
When SQLWARN0 is a W, DB2 also provides helpful information about the problem in
one or more of the other SQLWARNn fields. Also check warnings on every SQL statement
return.
For example:
Evaluate SQLCODE When 0 If SQLWARN0 = 'W' Display '*** Warning error ***' Display
'Sqlstate = ' Sqlstate End-If When Other ... End-Evaluate
12. Take advantage of the SQLERRD (3) out of the SQLCA.
The third occurrence of the SQLERRD array is one of the most useful fields in the SQLCA.
This field is populated after a successful insert, update, or delete with a count of the
number of rows inserted, updated, or deleted.
This is not populated when a mass delete with no WHERE logic is coded or populated
due to deletes affected by delete cascade.
13. Take advantage of fetching rowsets in your cursor processing. This should be strictly
enforced for large cursors because of the runtime savings.
14. Apply all calculations within the COBOL code and then move the value to a host variable.
Then reference the host variable in the SQL statement. Keep calculations out of SQL
statements whenever possible.
15. Hard code any and all values known within an SQL statement.
For example, if a program always processes the terminated rows on a table, then use the
SQL statement Where Status_Code = ‘T’. This is extremely helpful especially if frequency
value statistics are present for the different values of Status_Code in the catalog tables

You might also like