DB2

DB2 NULL Indicator Variables in COBOL: Preventing -305 and Preserving Meaning on FETCH

A DB2 column that is allowed to be NULL does not automatically have a safe COBOL representation. This guide shows how indicator variables prevent `SQLCODE -305` and, more importantly, how they help a batch or online program distinguish an absent value from a real zero, blank, or default.

ProgramPlanPackageDB2 access
Original DBala learning flow — a visual summary of this article’s practical decision path.

A DB2 column that is allowed to be NULL does not automatically have a safe COBOL representation. This guide shows how indicator variables prevent SQLCODE -305 and, more importantly, how they help a batch or online program distinguish an absent value from a real zero, blank, or default.

The support problem is not just the SQLCODE

A common incident starts after a table change or a new source feed begins leaving an optional column empty. A program fetches that column into a normal COBOL host variable and fails with -305: DB2 tried to put a NULL value into a variable that has no indicator variable.

The tempting response is to change the query to COALESCE(column, default) and rerun. That may remove the abend, but it can also erase business meaning. For example, an unknown credit limit is not necessarily the same as a credit limit of zero. A missing contact date is not the same as 0001-01-01, and an absent rate should not silently become 0.0000 if later calculations or reports treat zero as a valid value.

Treat NULL handling as a data-contract decision. First establish whether NULL is permitted by the table definition, whether the application regards it as meaningful, and what the receiving process must do with it. The example below is fictional and uses a customer preference extract; adapt names, copybook rules, and review steps to local standards.

A small model: value and indicator travel together

In embedded SQL, an indicator variable is a signed small integer associated with a host variable. For input, a negative indicator tells DB2 to store NULL. For output from a SELECT or FETCH, DB2 sets the indicator negative when the result is NULL. A nonnegative indicator means the host variable holds a value. For character data, a positive output indicator can also signal truncation and contain the original length, so it must not be ignored.

The safe mental model is:

DB2 nullable column
        |
        +-- non-NULL --> host variable contains business value
        |
        +-- NULL     --> indicator is negative; host variable is not the value to use

Do not test the host variable for spaces or zero to infer NULL. After a NULL fetch, any apparent host-variable content is not evidence that DB2 supplied a usable business value. The indicator is the evidence.

Worked COBOL cursor example

Suppose a monthly extract needs customer number, preferred contact channel, and an optional SMS_OPT_IN_DATE. A NULL date means the customer has not made an SMS preference decision. The report must label that condition explicitly rather than treating it as a date or rejecting the entire customer row.

       WORKING-STORAGE SECTION.
       01  WS-CUSTOMER-NO          PIC 9(9).
       01  WS-CONTACT-CHANNEL      PIC X(10).
       01  WS-SMS-OPT-IN-DATE      PIC X(10).
       01  WS-SMS-OPT-IN-IND       PIC S9(4) COMP.
       01  WS-EOF                  PIC X VALUE 'N'.

EXEC SQL
DECLARE C-PREF CURSOR FOR
SELECT CUSTOMER_NO,
CONTACT_CHANNEL,
SMS_OPT_IN_DATE
FROM CUSTOMER_PREF
WHERE ACTIVE_IND = 'Y'
ORDER BY CUSTOMER_NO
END-EXEC.

EXEC SQL
OPEN C-PREF
END-EXEC.

PERFORM UNTIL WS-EOF = 'Y'
EXEC SQL
FETCH C-PREF
INTO :WS-CUSTOMER-NO,
:WS-CONTACT-CHANNEL,
:WS-SMS-OPT-IN-DATE
:WS-SMS-OPT-IN-IND
END-EXEC

EVALUATE SQLCODE
WHEN 0
IF WS-SMS-OPT-IN-IND < 0
MOVE 'NOT DECIDED' TO WS-SMS-OPT-IN-DATE
ELSE
CONTINUE
END-IF
PERFORM WRITE-EXTRACT-RECORD
WHEN 100
MOVE 'Y' TO WS-EOF
WHEN OTHER
PERFORM SQL-ERROR-ROUTINE
END-EVALUATE
END-PERFORM.
```

The colon before the indicator is required because it is a host variable. Its placement after the nullable host variable associates the two values. The program checks SQLCODE before using the fetched row, then checks the indicator before applying date-specific logic.

This example uses PIC X(10) only to keep the focus on NULL behavior. A production program should use a host variable whose type and format match the DB2 column and the site’s date conventions. If the date is later passed to arithmetic, conversion, or an external file, define a separate output representation rather than assuming the fetched form is ready for every use.

Decide the business action before coding a default

The handling choice belongs to the field’s meaning, not merely its DB2 data type.

| Column meaning | NULL could mean | Safer application action |
|---|---|---|
| Optional preference date | No decision received | Keep the record; show an explicit status or leave the outbound field empty according to interface rules |
| Mandatory settlement amount | Required source data missing | Reject or hold the record; produce an exception count |
| Optional middle name | No value supplied | Render an empty display field; do not substitute a made-up value |
| Product rate used in calculation | Value not established | Stop or route for review unless an approved business default exists |

A SQL COALESCE expression can be right when the default is an agreed part of the data contract. For a display-only extract, COALESCE(MIDDLE_INITIAL, '') may be clear and appropriate. It is risky when used only to make -305 disappear. Put the reason for the default in the program specification or interface documentation, not only in a SQL expression.

Failure cases that look similar but need different fixes

-305 during FETCH. The selected column can be NULL, but the corresponding host variable has no indicator. Add the correctly declared indicator and decide the null branch. Confirm the program’s package was rebuilt through the approved change process; do not bind or deploy directly from a support console.

A null indicator exists but the program still uses the value. This is a logic defect. The indicator must be tested before formatting, calculation, validation, or output. A generic edit routine that treats low values, blanks, or zeros as a real value can turn a correct fetch into a misleading file.

An output indicator is positive. For variable-length character results, DB2 may set a positive indicator when truncation occurred. That is different from NULL. Capture the column definition, host-variable length, SQLWARN fields, and a masked sample before deciding whether to enlarge the receiving field or reject the row.

-407 on INSERT or UPDATE. This is the inverse direction: an application attempted to store NULL in a column DB2 does not allow to be NULL. Check the target column definition and the input indicator. Do not change a column to nullable merely to get a batch through; that changes the data model and may affect many consumers.

What to verify before rerun or promotion

Practical takeaway

An indicator variable is not just syntax added to silence -305. It is the program’s explicit signal that a DB2 field has no value. Carry that signal into the business decision, test both branches, and only rerun after the data meaning and prior commit position are understood.

← Back to all articles