SQL Programming Manual for Pascal

Using Dynamic SQL
HP NonStop SQL Programming Manual for Pascal—528614-001
7-20
Dynamically Allocating Memory
Allocating Memory for the SQLDAs and Names Buffers
In preparation for allocating memory to store the SQLDA structure, you must get the
number of input parameters or output variables from the SQLSA structure. To allocate
memory for the SQLDAs and names buffers for the input and output variables, you use
the MALLOC function. MALLOC (n) allocates a block of memory, n bytes in length,
from the heap and returns the address of that block. The value passed for n must be
defined as type LONGINT. The address returned is of type EXTADDR.
MALLOC is a low-level alternative to the standard Pascal NEW function. The
declaration for MALLOC resides in the $SYSTEM.SYSTEM.PASEXT module; the body
is in $SYSTEM.SYSTEM.PASRUN. You cannot use NEW because you do not know in
advance the data type of the information that the program will have to process; to use
NEW, Pascal’s strong type checking would require you to specify a data type for the
pointer before knowing the data type of the information to be accessed.
The program calls MALLOC and puts the values returned by MALLOC into pointers to
the SQLDA and names buffer variables that were defined earlier in the program.
Initializing EYE_CATCHER and IND_PTR
When you allocate the SQLDA, you must explicitly initialize the EYE_CATCHER and
IND_PTR fields. You must initialize IND_PTR even if your program is not using
indicator variables to handle null values.
If you use INCLUDE SQLDA, the Pascal compiler defines EYE_CATCHER as a
constant named SQLDA_EYE_CATCHER; if you explicitly define the SQLDA you must
include the constant declaration:
CONST SQLDA_EYE_CATCHER = 'D1';
D1 indicates Release 2 of NonStop SQL. Use the constant as follows:
sqlda_name.EYE_CATCHER := SQLDA_EYE_CATCHER;
You should initialize the IND_PTR field for each SQLVAR entry to NIL. For example:
FOR INDEX := 1 TO NUM_ENTRIES DO
sqlda-name.SQLVAR[INDEX].IND_PTR := RETYPE (NIL, EXTADDR);
Preparing to Allocate the SQLDA. In Figure 7-4 on page 7-21 ALLOCATE_SQLDA is
a memory allocation procedure; INPUT_SQLDA_PTR and INPUT_NAMESBUF_PTR^
are pointers to an input SQLDA and input names buffer: