SQLCODE +802: Arithmetic exception (warning)
SQLCODE +802 (SQLSTATE 01519) is a warning that an arithmetic expression in the outer SELECT list failed, for example with an overflow or a division by zero. Db2 sets that expression's indicator variable to -2, returns the other columns, and keeps any cursor open.
Other names for this code
Also written as SQLCODE 802, SQLCODE=+802, SQLCODE = 802, sqlcode +802, db2 +802, sqlstate 01519, db2 802 zero divide, SQL0802.
What it means
Arithmetic in SQL can fail. A sum can overflow its data type, a decimal result can lose digits, and a division by zero has no answer. Where the expression sits decides what Db2 does.
- If the failing expression is in the SELECT list of the outer SELECT and has an indicator variable, Db2 returns +802. That expression comes back with an indicator of -2, and every other column is returned as normal.
- Anywhere else, such as a WHERE clause, a SET clause or an aggregate, or without an indicator variable, the statement fails with -802. Its SQLSTATE is 22012 for a zero divide and 22003 for the rest.
The message names the exception type, the operation and the data type. Exception types include DECIMAL OVERFLOW, DIVIDE EXCEPTION, EXPONENT OVERFLOW, FIXED POINT OVERFLOW, OVERFLOW, UNDERFLOW, OUT OF RANGE and ZERO DIVIDE. Operations are addition, subtraction, multiplication, division, negation or a built-in function. The data type shown can be the type of Db2's internal copy rather than the column's own type. Division by zero on DECFLOAT data doesn't raise this exception.
On Db2 for i, SQL0802 means "Data conversion or data mapping error" and can come with +802 or -802. It has its own list of error types, so read the cause text in the job log rather than applying the z/OS meaning.
Common causes
- A division where the divisor column is zero for some rows, such as AMOUNT / QUANTITY.
- An addition or multiplication in the SELECT list that grows past the range of INTEGER or the precision of a DECIMAL result.
- Multiplying two DECIMAL columns, which can give a result with more digits than Db2 can carry.
- An expression on a SMALLINT or INTEGER column that overflows on large values.
- Bad data, such as a huge or zero value that shouldn't be in the column at all.
How to find the problem
- Read the exception type, operation, data type and position number. The position is the expression's place in the SELECT list.
- Find rows where that expression's indicator came back as -2. Those rows have the values that caused it.
- For zero divide, query the divisor column for zeros. For overflow, look at the largest values involved.
- Check the ranges of the numeric data types involved against the values in the table.
How to fix it
- Guard divisions with a CASE expression, for example
CASE WHEN QUANTITY = 0 THEN NULL ELSE AMOUNT / QUANTITY END. - Cast to a wider type before the arithmetic, such as BIGINT, or DECIMAL with more digits.
- Fix bad data at its source if zero or huge values shouldn't exist.
- Keep indicator variables on every expression in the SELECT list, and treat -2 as an error in the program.
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 +802 instead of -802?
The failing expression was in the outer SELECT list and had an indicator variable, so Db2 could flag just that value. In other places, or without an indicator, the same problem gives -802.
Is the cursor still usable after +802?
Yes. Db2 returns the other columns and the cursor stays open.
What does an indicator of -2 mean here?
The expression couldn't be computed for that row. The host variable doesn't hold a usable value.