Db2 SQLCODE

SQLCODE -302: Input value invalid or too large

SQLCODE -302 means the value in an input host variable is too long or too large for the column or value it's being assigned to, or isn't valid data for its type. SQLSTATE 22003 means a number is out of range. 22001 covers the other cases, such as a string that's too long or a packed decimal field holding bad data.

Also written as SQL -302, SQLCODE=-302, SQLCODE = -302, db2 -302, sqlstate 22001, sqlstate 22003, db2 value too large for column.

What it means

Db2 checks the value of every input host variable before it uses it. If a value won't fit where the statement is putting it, Db2 stops the statement with -302 rather than cut the value short or change it. Trailing spaces are the exception. Db2 drops them quietly when a string is stored in a column, so only the characters before them count. The message tells you which host variable it was by number. Host variables are counted from 1, left to right, in the order they appear in the statement.

The SQLSTATE narrows it down. 22003 means a number doesn't fit, such as 70000 going into a SMALLINT column, or 123456.78 into a DECIMAL(7,2) column whose largest value is 99999.99. 22001 is used for everything else. Usually that's a character string longer than the target can hold. It's also what you get when a DECIMAL host variable doesn't contain valid packed decimal data.

Only the failing statement is rejected. Changes made earlier in the unit of work stay in place until the program commits or rolls back.

Common causes

  • A character host variable is longer than the column, and for this record it holds more characters than the column can store, for example a 35-character name going into CHAR(30).
  • The length field of a VARCHAR host variable (the 49-level field in COBOL) is set larger than the column, and the text past the column's length isn't all spaces. Leftover data from an earlier record is a common reason.
  • A number is too big for the column type, such as a PIC S9(9) COMP field holding 70000 inserted into a SMALLINT column.
  • An amount has more integer digits than the DECIMAL column allows, for example a total that has grown past what DECIMAL(7,2) can hold.
  • A program field was widened in a change, but the table column wasn't widened to match.
  • A packed decimal (COMP-3) host variable holds spaces, low-values or other bytes that aren't a valid packed number. Db2 reports this as -302 with SQLSTATE 22001.

How to find the problem

  1. Read the number in the message. Count the host variables from left to right in the statement to find which one it is.
  2. Use the SQLSTATE to decide what to look for. 22003 points to a number out of range. 22001 points to a long string, or to bad data in a packed decimal field.
  3. Compare the host variable's definition with the column in SYSIBM.SYSCOLUMNS. COLTYPE, LENGTH and SCALE give the column's type and size. For DECIMAL, LENGTH is the precision.
  4. Display the value, and for VARCHAR the length field too, for the record that failed. Show a packed decimal field in hex. Every half-byte should be 0 to 9 except the last, which holds the sign (C, D or F).

How to fix it

  • Set the VARCHAR length field to the real data length before every INSERT or UPDATE, for example by working it out from the value with trailing spaces removed.
  • Check the value in the program before the SQL runs, and report the bad record instead of failing the run.
  • If valid data has outgrown the column, have the DBA widen it with ALTER TABLE, then rebind the programs that use it.
  • Keep host variables in step with the table. Generating them with DCLGEN after every table change avoids most mismatches.

What it looks like

CUSTUPD - UPDATE CUSTOMER FAILED, CUST-NO 0081234
DSNT408I SQLCODE = -302, ERROR:  THE VALUE OF INPUT VARIABLE OR PARAMETER NUMBER 3 IS
         INVALID OR TOO LARGE FOR THE TARGET COLUMN OR THE TARGET VALUE
DSNT418I SQLSTATE   = 22001 SQLSTATE RETURN CODE

Questions

Why does Db2 fail instead of truncating the value?

Cutting the data short would lose information without anyone knowing. Db2 refuses the assignment so the program can decide what to do with the record.

What's the difference between -302 and -311?

-311 means the length in a VARCHAR host variable's length field is negative or bigger than the variable itself. -302 means the value is too long or too large for the column it's going into.

Does -302 roll back my earlier updates?

No. Only the failing statement is rejected. Earlier changes stay until the program commits or rolls back.