Learn it on Mainframe Path. Do it with MainframeKit.Learn on Mainframe Path ▸
MainframeKit

-311

Host variable length negative or too largeNEGATIVE SQLCODE

An input host string variable had a length that was negative or above the allowed maximum, so the statement could not run.

MOST LIKELY

A length field calculated from other values that went below zero.

FIRST CHECK

Note the position number in the message and find the matching host variable in the statement, counting from 1.

TRY IT ▸

Site search

What it means

Input host string variables are described to Db2 by a length, which sits in the SQL descriptor area (SQLDA). When Db2 evaluates the statement it reads that length. A negative value, or one above the allowed maximum, cannot describe a real string, so Db2 stops the statement. The message gives the position of the variable in the SQLDA, which tells you which input is at fault.

The cause is usually in the program rather than in the table data. The length often comes from a computed field, a moved value or an uninitialised variable, so check how it is set before the statement runs.

Typical causes

  • A length field calculated from other values that went below zero.
  • A length moved from a field that was never initialised.
  • A length above the allowed maximum, for example from a field sized wrongly.
  • Dynamic SQL where the program builds the length values itself.
  • A length stored at the wrong offset in a structure, so a different value is read.

How to fix it

  1. Note the position number in the message and find the matching host variable in the statement, counting from 1.
  2. Display that length value just before the statement runs.
  3. Initialise length fields and check the arithmetic that sets them.
  4. Correct the program so that no negative or oversized length is ever passed.
  5. Rerun the program after the correction.

Example

Illustrative only; names and values are invented.

SQLCODE = -311, input host variable number 3 has a negative length
-- WS-NAME-LEN computed as WS-END-POS - WS-START-POS gave -4