You are on page 1of 7

-305 db2 Null indicator

How to Handle the DB2 sql Code -305 �in cobol-DB2 Application Program
?
In DB2, the columns defined as�NULL�needs to be handled carefully else it will throw
null exception error, in order to over come this error�data type can be handled by
using�null indicator.

 NULL is stored using a special one-byte null indicator that is “attached” to every
nullable column.

 If the column is set to NULL, then the indicator field is used to record this.

 Using NULL will never save space in a DB2 database design – in fact, it will always
add an extra byte for every column that can be NULL. The byte is used whether or
not the column is actually set to NULL. The indicator variable is transparent to an
end user

Consider below Table :

Create Table SAMP_TAB

SN CHAR (10)

SNAME CHAR (10)

STATUS CHAR (2) NOT NULL BY DEFAULT

CITY CHAR (10) NOT NULL

Note :: Unless you specify NOT NULL, the default is to allow for�NULL
In above table SN and SNAME columns holds null values by default, in order to handle
these null variables we need to have NULL-INDICATORS declares in the Program
as�S9(4) comp�variable (A indicator variable is shared by both the database
manager and the host application. Therefore, this variable must be declared in the
application as a host variable, which corresponds to the SQL data type SMALLINT)
Let us declare the Null indicators for above two variables in application program as

02 SNAME-INDNULL S9(4) comp

05 SN-IN S9(4) comp

What values Null indicators will hold :�


1. �-1� : Field is having NULL value
2. ��0� : Field is�Not�NULL value
3. �-2� : Field value is truncated�
How /Why to handling Null Values:
 When processing INSERT or UPDATE� statements, the database manager checks
the null-indicator variable, if one exists. If the indicator variable is negative, the
database manager sets the target column value to null, if nulls are allowed else it
throws�sql error code�-305, we need null indicators to handle this situation.
 If the null-indicator variable is zero or positive, the database manager uses the value
of the associated host variable.

There are two reasons for getting -305 and Resolution :


1)�If the table column is defined as NOT NULL (with no default) and if we try
to�insert�a null value we get this error.
Resoulution :This should be resolved by making sure that the inserted value is not
null. Null indicator cannot be used here since the column is defined as NOT NULL.
(validate the data, if its not numeric or less than spaces then move spaces into it and
then insert or update into table)

2)�A table column is defined as NULL, The host variable has a not null value and the
Null indicator is not set in the host program, so the null indicator is defaulted to a
negative value.

Resoulution :This should be resolved by using a null indicator in the host program
and moving the relevant value to the null indicator. Here inorder to move null value into
respective column nove -1 to null indicator.
Eg :

MOVE -1 to SNAME-INDNULL
EXEC SQL INSERT INTO SAMP_TAB

(SN,SNAME,STATUS,CITY) VALUE

(:SN,:SNAME:SNAME-INDNULL,:STATUS,:CITY)
END-EXEC

Eg :

EXEC SQL SELECT SNAME

INTO :SNAME:SNAME-INDNULL

FROM SAMP_TAB

WHERE SN =�:SN-IN
END-EXEC

(Note :�If SNAME has a value, SNAME-INDNULL contains 0.

If SNAME is NULL, SNAME-INDNULL contains -1. )

If SQL-CODE = -305 and SNAME-INDNULL = -1

Display � SNAME is having null values �

Else

End-If

Important Points wrt NULL Variables


 NULLs can present problems because they are handled differently by different
computers and the collating sequence is inconsistent with regard to NULLs.

 Unless you specify NOT NULL, the default is to allow for NULLs

 It’s easy for us to get lazy and allow columns to contain NULLs when it would be
better to specify NOT NULL

 Remember to allow for NULLs creating UNKNOWN logical values. Always test your
code with NULLs in all possible places.

 The NULL is a global creature, not belonging to any particular data type, but able to
replace any of their values.

 A NULL isn’t a zero, it isn’t a blank string, it isn’t a string of length zero.

 The basic rule for math with NULLs is that they propagate. An arithmetic operation
with a NULL will return a NULL. If you have a NULL in an expression, the result will
be NULL.

 If you concatenate a zero length string to another string, that string stays the same. If
you concatenate a NULL string to a string, the string becomes a NULL.

 In comparisons, the results can be TRUE, FALSE, or UNKNOWN. A NULL in a row


will give an UNKNOWN result in the comparison.

 Sometimes negating the wording of the problem helps. Instead of saying “Give me
the cars that met all the test criteria,” say “Don’t give me any car that failed one of
the test criteria.” It is often easier to find what you do not want than what you do
want. This is very true when you use the NOT EXISTS, but beware of NULLs and
empty tables when you try this.

 You can’t completely avoid NULLs in SQL. However, it is a good idea to try as hard
as you can to avoid them whenever possible.
 Make yourself think about whether you really need NULLs to exist in a column
before you omit the NOT NULL clause on the column definition.

 Use NULLs sparingly

———————————————————————————————–

If you get -305 sqlcode which means you got null values into the host variables.

Solution –
Use NULL INDICATOR VARIABLES to stop getting -305.

Example
———–

,: FIRST-NAME :WS-FIRST-NAME-NULL-IND
,: INIT-NAME :WS-MID-NAME-NULL-IND

FROM EMPLOYEE ……..

Define Null Indicator variables as follows


05 WS-FIRST-NAME-NULL-IND PIC S9(04) COMP.

Use following logic When SQLCODE = 0.

IF WS-FIRST-NAME-NULL-IND >= 0
… Means Field populated with proper value
ELSE
… Means Field populated with null value
END-IF

In sum cases, I would suggest handle -305 instead of using


null indicator variables. If you are feching only one field, in that
case better go and handle -305, if query hit -305 move space/zero’s
into host variable.

Q. �� -305 DB2 SQL RETURN CODE ? Full Explanation


:
���DB2 SQL return code -305 – Null indicator needed.
Lets go depper ?
-305: The NULL value cannot be assigned to output host
variable number because no indicator variable is specified.
Can?t Understand. See below for example,
DB2 IN COBOL:
EXEC SQL
SELECT NAME
INTO :HOST-NAME
FROM TABLE_ID
WHERE ID = 25.
Here, NAME column is having NULL for ID = 25 . And NULL value can?t be moved to
the variable HOST_NAME , so will result in Cobol db2 return code -305.
So whenever column is NOT NULL column , mostly we won?t have this COBOL SQL
code -305. I have used the word mostly because in some cases even NOT-NULLABLE
column may result in -305 return code. See the example below
EXEC SQL
SELECT MAX(TOTAL)
INTO :HOST_TOTAL
FROM TABLE_MARKS
WHERE SCIENCE = 100.
Here although column TOTAL is not null , it will result in -305 when there is no rows for
the condition.
How can we avoid SQL return code -305 in cobol program:
The best way is to use NULL indicator in DB2 SQL query as in below example,
EXEC SQL
SELECT MAX(TOTAL)
INTO :HOST_TOTAL:WS-NULL
FROM TABLE_MARKS
WHERE SCIENCE = 100.
So here NULL variable will be set to -1 when the result to the corresponding host
variable is NULL.
Q) How do you retrieve the data from a nullable column?
� � �Use null indicators. Syntax … INTO :HOSTVAR:NULLIND
Q) What is the picture clause of the null indicator variable?�
� � � �S9(4) COMP.
Q) What does it mean if the null indicator has -1, 0, -2?�
� � � �-1 : the field is null
� � � �0 : the field is not null
� � � �-2 : the field value is truncated
Q) How do you insert a record with a nullable column?
� � � To insert a NULL, move -1 to the null indicator
� � � To insert a valid value, move 0 to the null indicator