DB2

DB2 SQLCODE -811 in COBOL Batch: Prove the Control-Row Rule Before Changing SELECT INTO

A batch program that expects one control row can fail with SQLCODE `-811` when its `SELECT INTO` finds two or more rows. The immediate failure is clear; the safe correction depends on proving which row, if any, was supposed to be unique.

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

A batch program that expects one control row can fail with SQLCODE -811 when its SELECT INTO finds two or more rows. The immediate failure is clear; the safe correction depends on proving which row, if any, was supposed to be unique.

The example names and values below are fictional. DB2 release levels, isolation rules, indexes, change controls, and production recovery standards vary. Do not add FETCH FIRST 1 ROW ONLY, delete a row, or rerun an updating job merely to suppress -811.

The symptom is a broken business assumption

A typical batch job reads a parameter for a business date, interface type, or processing cycle before it starts its detail work. The program uses a singleton SELECT INTO because its design assumes one active control row.

           EXEC SQL
              SELECT RUN_DATE, CUTOFF_TIME
                INTO :WS-RUN-DATE, :WS-CUTOFF-TIME
                FROM BATCH_CONTROL
               WHERE PROCESS_NAME = :WS-PROCESS-NAME
                 AND CONTROL_STATUS = 'A'
           END-EXEC

EVALUATE SQLCODE
WHEN 0
CONTINUE
WHEN 100
PERFORM CONTROL-ROW-MISSING
WHEN OTHER
PERFORM DB2-ERROR
END-EVALUATE
```

If two rows meet the predicate, DB2 returns -811: the result of a SELECT INTO was more than one row. This is not the same condition as SQLCODE +100, where no row was found. It says the program’s single-row contract and the stored data no longer agree.

Treat the contract as the subject of the investigation. The question is not “How can the program pick a row?” It is “What business rule should have prevented more than one active row, and did the job already act before discovering the breach?”

Capture evidence before someone changes the rows

Start with the failing job’s identity and scope. Preserve enough information to reproduce the query meaning without exposing sensitive data in broad support channels.

Collect:

The input value matters. A program receiving DAILY-POST is not necessarily asking the same question as one receiving DAILY-POST in a fixed-length host variable, particularly when a predicate applies functions or compares differently typed values. Do not infer the value from a scheduler label alone.

Diagnose the intended uniqueness rule

Run read-only diagnostic SQL in an approved environment or through the site’s controlled query process. Use the same predicate first, then widen it only to understand why the rows qualify.

SELECT PROCESS_NAME,
       CONTROL_STATUS,
       RUN_DATE,
       CUTOFF_TIME,
       EFFECTIVE_FROM,
       EFFECTIVE_TO,
       LAST_UPDATE_TS
  FROM BATCH_CONTROL
 WHERE PROCESS_NAME = :process_name
   AND CONTROL_STATUS = 'A'
 ORDER BY LAST_UPDATE_TS DESC;

For illustration, suppose it returns these two rows:

| PROCESS_NAME | STATUS | RUN_DATE | EFFECTIVE_FROM | EFFECTIVE_TO |
|---|---|---:|---|---|
| DAILY-POST | A | 2026-11-20 | 2026-11-20 | null |
| DAILY-POST | A | 2026-11-19 | 2026-11-19 | null |

That result supports several very different explanations:

| Observation | Likely interpretation | Safe next action |
|---|---|---|
| An old row and a new row are both active | A close-and-open process was incomplete | Trace the approved control-maintenance transaction and audit history. |
| Duplicate rows have identical business values | A load, replay, or manual insert may have duplicated a control event | Identify the source transaction before any correction. |
| Rows differ by region, company, or cycle but the program does not predicate on it | The SQL may be missing a required business discriminator | Review program specification and callers; test a controlled code change. |
| Multiple rows are intentionally valid over time | SELECT INTO is the wrong access pattern or effective-date predicate | Define selection rules with the business owner, then redesign and test. |

The table definition is evidence, not a verdict. A unique index on (PROCESS_NAME, RUN_DATE) would still allow two active rows for different dates. Conversely, a unique key on PROCESS_NAME might be inappropriate if the real model legitimately retains multiple dated versions. Do not propose an index until the business cardinality is known.

A tempting SQL change that can create a silent error

Someone may suggest this change:

SELECT RUN_DATE, CUTOFF_TIME
  INTO :WS-RUN-DATE, :WS-CUTOFF-TIME
  FROM BATCH_CONTROL
 WHERE PROCESS_NAME = :WS-PROCESS-NAME
   AND CONTROL_STATUS = 'A'
 FETCH FIRST 1 ROW ONLY;

It prevents -811, but without an ORDER BY it does not express which row is correct. Even with an ORDER BY, it encodes a policy such as “most recently updated wins.” That can be valid only if the owner confirms that policy, the data model permits it, and regression tests prove it does not select an unfinished future cycle.

A cursor is similarly not a cure by itself. Use a cursor when the application genuinely needs to process a set. Do not switch to a cursor merely to accept an invalid singleton-control condition one arbitrary row at a time.

Safe correction paths

Choose the correction based on the evidence rather than the SQLCODE alone.

Data-maintenance defect. If the rule is exactly one active row and history shows an interrupted or duplicate maintenance action, use the organization’s approved data-fix process. The fix should identify the correct row, record who authorized the outcome, and preserve auditability. A direct delete may remove evidence needed to understand how the duplication occurred.

Application predicate defect. If the program omitted a mandatory qualifier such as cycle, region, or effective date, treat it as a code change. Update the program and any copybook or calling convention involved, then compile, bind, and test under normal controls. Check related online or batch consumers: they may contain the same incomplete predicate.

Data-model/control gap. If multiple active rows must never coexist, a preventive constraint, unique index, or controlled maintenance procedure may be appropriate. If they may coexist, document the selection rule explicitly. In either case, coordinate with database administration and the business owner; a uniqueness change can fail existing loads or maintenance jobs.

Before rerun: protect against duplicate business work

A failed control lookup does not prove the job made no changes. The lookup may occur after setup, after an initial commit, or after a previous restart point.

Before authorizing a rerun, verify:

1. The failing execution’s step and program level are known, and the corrected control-row state is independently reviewed.
2. Application logs, checkpoint records, output data sets, and target-table audit columns show whether detail processing or outbound delivery began.
3. Any restart parameter points to a documented safe boundary; it does not simply repeat a completed update range.
4. The intended control row is stable for the rerun window, including effective-date and cutoff-time rules.
5. Reconciliation controls—input count, processed count, rejected count, amount/hash total where applicable—have an expected baseline and an owner to review them.
6. A -811 from a different control lookup cannot be masked by the same rerun change.

If the job produced an interface file or committed detail rows, involve the application owner and operations team before restarting. Local standards may require a compensating process, a new cycle identifier, or a formally approved restart rather than a rerun.

Practical takeaway

SQLCODE -811 is valuable evidence that a supposedly singular decision is ambiguous. Preserve the failing input and execution state, prove the intended control-row rule, correct the responsible data, program, or design control, and only then make a restart decision that protects against duplicate processing.

← Back to all articles