How to Resolve -305 SQL Error in DB2 Quickly

Here is a resolution for -305 SQL error in DB2. The handling of Nulls is needed when aggregate functions are involved in SQL Query. Null means no data is returned.

Advertisements

How to Solve -305 SQL Error

You need to define indicator variable (NULL indicator) in SELECT statement of SQL if that contains one of the aggregate functions (MIN MAX, AVG, SUM).

DB2 write a value to the indicator variable if it finds no data to process. Here are two resolutions. If the program is not set up with a indicator variable, an invalid -305 SQLCODE is returned.

Resolution-1

In the COBOL program you need a logic to validate indicator variable . Here is sample code how to create indicator variable.

If the indicator variable value is less than zero, which means it is NULL value. If it is positive integer means, the data is truncated. Here are the conditions to check indicator variables.

Resolution-2

It is preferable to code the VALUE, COALESCE, or IFNULL function to alleviate any null indicator logic.

For example:

SELECT IFNULL(AVG(SALARY), 0) 
FROM EMP 
WHERE WORKDEPT = 'XYZ'

This will either return the average if rows are found, and zero if no rows were met in order to calculate an average.

Related Posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.