SQL Programming Manual for Pascal
Using Dynamic SQL
HP NonStop SQL Programming Manual for Pascal—528614-001
7-14
Dynamically Allocating Memory
Using a statement host variable:
EXEC SQL EXECUTE :STATEMENT_HOSTVAR
8. End the TMF transaction (for both SELECT and other statements):
EXEC SQL COMMIT WORK;
9. Deallocate the memory for the SQLDAs and names buffers and for the values (the
example uses procedures DEALLOCATE_SQLDA and DEALLOCATE_SPACE to
deallocate memory and declares HEAP_POINTER in the main code).
The following paragraphs describe some of these steps in detail.
Dynamically Allocating Memory
A Pascal program can dynamically allocate memory for input parameters and output
variables at run time when the parameters and variables are known. To dynamically
allocate memory:
1. Issue the ?XMEM directive which tells the compiler to use the large-memory model
often necessary to allocate storage for large numbers of variables.
2. Set up your program to circumvent Pascal type checking (for handling input and
output variables) and bounds checking (for the SQLDA definition).
3. Declare an SQLSA by issuing the INCLUDE SQLSA directive.
4. PREPARE the input statement.
5. Use the information in the SQLSA to determine the number of input parameters
and output variables in the statement.
6. Allocate space for the required number of SQLDA entries to describe the
parameters and output variables, using the MALLOC system procedure.
7. Allocate space for the values input to the program or output from the database,
again using MALLOC.
These steps are described in order on the following pages.
Issuing the ?XMEM Directive
The ?XMEM directive should appear on the first line of the source program to direct the
compiler to use the large-memory model.
Circumventing Type Checking and Bounds Checking
To circumvent Pascal’s strong typing and make it easier to do dynamic SQL
programming, you can either use a record variant declaration or use the RETYPE
function. The following paragraphs show how to use both techniques.