SQL Programming Manual for Pascal

Using Dynamic SQL
HP NonStop SQL Programming Manual for Pascal528614-001
7-36
Handling Null Values
{ according to flag }
END;
EXEC SQL OPEN : cursor-host-variable;
{ Loop until SQLCODE = 100: }
BEGIN
EXEC SQL FETCH : cursor-host-variable INTO : column-
hostvariables;
{ Display column values }
END;
EXEC SQL CLOSE : cursor-host-variable;
Handling Null Values
The input and output SQLDA structures have two fields, NULL_INFO and IND_PTR,
that are used for handling null values. Your program accesses these fields in the
SQLVAR array, in the same way in which you access VAR_PTR.
NULL_INFO tells whether or not the input parameter or output variable can contain
a null value.
IND_PTR points to a flag that tells whether the input parameter or output variable
actually is null. If the parameter or output variable is not null, VAR_PTR points to
the value.
If NULL_INFO is 0, IND_PTR has no meaning.
If NULL_INFO is less than 0, your program should access IND_PTR before every
access of VAR_PTR in order to handle null values correctly.
Handling Null Values in Input Parameters
If your program is to handle null values on input, each parameter in the statement
entered by the user or constructed by your program must have a corresponding
indicator parameter, or a run-time error will occur when a null value is encountered.
After DESCRIBE INPUT executes and for each input parameter described in an
SQLVAR array in the input SQLDA, SQL sets NULL_INFO to -1 if the input parameter
in the prepared statement could have a null value (that is, if the prepared statement
included a null indicator).
If NULL_INFO contains a value of -1 and you are allocating memory dynamically, you
can now allocate 2 bytes of memory for a null indicator value, and then set IND_PTR to
point to the memory. Allocate this memory at the same time you allocate memory for a
possible nonnull parameter value.
If the user specifies a null value for the parameter, you assign a -1 to the location
IND_PTR points to. SQL checks the value IND_PTR points to and assumes a null
value for the parameter.
If instead the user does not enter a null value for the input parameter, you can assign a
0 to the location IND_PTR points to. SQL checks IND_PTR, sees that IND_PTR