SQLCODE +304: Value out of range for host variable (warning)
SQLCODE +304 (SQLSTATE 01515) is a warning that a value Db2 fetched was out of range for the host variable meant to receive it. Db2 skips that one value, sets its indicator variable to -2, and returns the rest of the row.
Other names for this code
Also written as SQLCODE 304, SQLCODE=+304, SQLCODE = 304, sqlcode +304, db2 +304, sqlstate 01515, indicator variable -2, db2 304 warning.
What it means
When a FETCH or SELECT INTO puts a value into a host variable, the variable has to be able to hold it. A DECIMAL(11,2) value of 1,500,000.00 won't fit in a halfword binary (PIC S9(4) COMP) variable, for example. When the value doesn't fit, Db2 has two choices.
- If the host variable has an indicator variable, Db2 can mark just that item as unusable. It sets the indicator to -2, leaves that value out, returns every other column, and gives SQLCODE +304.
- Without an indicator variable, there's nowhere to report the problem for that one column. The whole statement fails with SQLCODE -304 (SQLSTATE 22003) and no data is returned.
An indicator of -2 is different from -1. -1 means the column really is null. -2 means Db2 had a value but couldn't hand it over, so the host variable doesn't hold anything you should use.
The message names the position of the item in the host variable list and both data types, the value's and the host variable's.
Common causes
- A host variable is smaller than the column, such as a SMALLINT or PIC S9(4) COMP variable for an INTEGER or DECIMAL column.
- The column was widened with ALTER TABLE, and the program still uses the old copybook or DCLGEN.
- A computed expression in the SELECT list, such as a SUM or a product of two columns, produces a value larger than the host variable allows.
- The DCLGEN was generated from a different table or view from the one the program now reads.
How to find the problem
- Note the position number and the two data types in the message. The position is the item's place in the INTO list.
- Compare that host variable's definition with the column in SYSIBM.
SYSCOLUMNS, or with the expression in the SELECT list. - Find rows where the indicator came back as -2. Those rows hold the values that don't fit.
- Check when the table definition and the DCLGEN were last changed.
How to fix it
- Make the host variable big enough for the full range of the column or expression.
- Generate the DCLGEN again after any change to the table, and recompile every program that uses it.
- Treat an indicator of -2 as an error in the program, not as a null, so bad rows aren't processed silently.
- If a smaller value is really wanted, cast or round it in the SELECT list instead of relying on the host variable.
IBM documentation
IBM Db2 for z/OS documentation. Use the documentation for your installed release.
This link opens the relevant IBM reference library or lookup instructions. Match the complete code and your installed product release; it is not a code-specific review.
Related codes
Questions
Why did I get +304 instead of -304?
Because the host variable had an indicator variable. Db2 could flag just that value with -2 and return the rest of the row. Without an indicator the same problem gives -304.
Is the host variable updated after +304?
No usable value is returned for that item. Check the indicator and don't use the host variable when it's -2.
What is the difference between indicator -1 and -2?
-1 means the column is null in the table. -2 means Db2 had a value but couldn't return it because of an error such as +304 or +802.