Need a ONLINE MAINFREAME Course/Poject Case Study) / Corporate Training/ application support ????

Lets schedule the class. Write me @, for more information refer to :
Iam Willing to take up Freelancing projects on Cobol / MF. please mail me @


Monday, February 25, 2008


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)




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 :






Eg :






(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 “





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


Aji said...

Right on point, I got what I was looking for. Thanks for the post.

vab said...

nice article.. well explained.

Ramya said...

Well explained. Thank you.

infocrossing said...

Very Good.

J.Guna S/o Arunadevi said...

I got the details what i was exactly searching for. Thanks for the post dear!!

Ananth TM said...

just as everyone says... to the point.. what we really want

Anonymous said...

well explained ... Thank u

Ryan said...

Check this

Rikdeb Mukherjee said...

Check out the mainframe blog

Anonymous said...

correct definition for indicator variable:
02 SNAME-INDNULL PIC S9(4) comp.

Unknown said...

Thanks but I am no more into Mainframes

Anonymous said...

good explanation

himanshu koolwal said...

Very well explained.. Can you please explain why we might be needing some columns to contain Null values ? Why not declare every column as Not Null.. I understand it will take one byte extra for each column but won't it be a effective way to reduce the errors ?

Diego Matos said...

Thanks, well explained.
I've been looking for this solution for days because a variable in cobol is never null but I needed this representation in DB2 to bring all the records in my query.