SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)

Dynamic SQL With Descriptor Areas
HP NonStop SQL/MX Programming Manual for C and COBOL523627-004
10-14
Declare a Host Variable for the Dynamic SQL
Statement
4. Prepare the SQL Statement on page 10-15
5. Describe the Input Parameters and the Output Variables on page 10-15
6. Set Explicit Input Values on page 10-16
7. Execute the Prepared Statement on page 10-18
8. Get the Count and Descriptions of the Output Variables on page 10-19
9. Deallocate the Prepared Statement and the SQL Descriptor Areas on page 10-20
Declare a Host Variable for the Dynamic SQL Statement
In an SQL Declare Section, declare a host variable to use as a container for the
dynamic SQL statement. Specify this host variable when you prepare the SQL
statement. You must declare the host variable:
Before the PREPARE statement
Within the same scope as the PREPARE statement
Construct the SQL Statement From User Input
Construct the SQL statement from user input and store the statement in the host
variable. For example, the host variable named hv_sql_statement might contain a
statement of this form:
SELECT column-list
FROM table
WHERE column = CAST(? AS S)
The variables (specified in italics) in the preceding SELECT statement represent
character strings to be provided by the user.
Example
After specifying the table name, the column name, and the data type to be used for row
selection, the host variable hv_sql_statement might contain this statement:
SELECT EMPNUM, FIRST_NAME, LAST_NAME, SALARY
FROM SAMDBCAT.PERSNL.EMPLOYEE
WHERE EMPNUM = CAST(? AS NUMERIC(4) UNSIGNED)
Allocate Input and Output SQL Descriptor Areas
You must allocate an input SQL descriptor area for dynamic input parameters and an
output SQL descriptor area for values of output variables (typically, columns of a
SELECT statement). See Allocating an SQL Descriptor Area on page 10-3.
Allocating the Input SQL Descriptor Area
The number of item descriptor areas for the input SQL descriptor area must be large
enough to accommodate all the dynamic input parameters of the prepared statement.