informix
INFORMIX-ESQL/C Programmer's Manual
Using a System-Descriptor Area

Handling a Parameterized SELECT Statement

For an introduction on how to determine input parameters, see page 15-28. This section describes how to handle a parameterized SELECT statement with a system-descriptor area. If a prepared SELECT statement has a WHERE clause with input parameters of unknown number and data type, your ESQL/C program must use a system-descriptor area to define the input parameters.

To use a system-descriptor area to define input parameters for a WHERE clause

  1. Determine the number and data types of the input parameters of the SELECT statement. For more information, see Determining Unknown Input Parameters.
  2. Allocate a system-descriptor area and assign it a name with the ALLOCATE DESCRIPTOR statement. For more information on ALLOCATE DESCRIPTOR, see Allocating Memory for a System-Descriptor Area.
  3. Indicate the number of input parameters in the WHERE clause with the SET DESCRIPTOR statement, which sets the COUNT field.
  4. Store the definition and value of each input parameter with the SET DESCRIPTOR statement, which sets the DATA, TYPE, and LENGTH fields in the appropriate item descriptor:
  5. Pass the defined input parameters from the system-descriptor area to the database server with the USING SQL DESCRIPTOR clause.
  6. The statement that provides the input parameters depends on how many rows that the SELECT statement returns. The following sections discuss how to execute each type of SELECT statement.

  7. Deallocate the system-descriptor area with the DEALLOCATE DESCRIPTOR statement. For more information, see Freeing Memory Allocated to a System-Descriptor Area.
  8. Important: If the SELECT statement has unknown columns in the select list, your program must also handle these columns with a system-descriptor area. For more information, see Handling an Unknown Select List.

The following sections demonstrate how to use a system-descriptor area to handle a parameterized SELECT statement that returns many rows and one that returns only a single row.

Executing a Parameterized SELECT That Returns Multiple Rows

The sample program described on the following pages shows how to use a dynamic SELECT statement with the following conditions:

This program is a version of the demo4.ec sample program; demo4 uses a system-descriptor area for select-list columns while this modified version of demo4 uses a system-descriptor area for both select-list columns and input parameters of a WHERE clause.

Lines 8 to 14

These lines declare host variables to hold the data obtained from the user and the column values retrieved from the system descriptor.

Lines 17 to 25

The lines assemble the character string for the statement (in demoquery) and prepare it as the demoid statement identifier. The question mark (?) indicates the input parameter in the WHERE clause. For more information on these steps, see Assembling and Preparing the SQL Statement.

Line 26

This line declares the democursor cursor for the prepared statement identifier demoid. All nonsingleton SELECT statements must have a declared cursor.

Line 27

To be able to use a system-descriptor area for the input parameters, you must first allocate the system-descriptor area. This ALLOCATE DESCRIPTOR statement allocates the demodesc system-descriptor area. For more information on ALLOCATE DESCRIPTOR, see Allocating Memory for a System-Descriptor Area.

Lines 28 to 38

These lines simulate the dynamic entry of the input parameter value. Although the parameter value is hard-coded here (line 37), the program would more likely obtain the value from user input. Line 38 simulates code that would determine how many input parameters exist in the statement string. If you did not know this value, you would need to include C code at this point to parse the statement string for the question mark (?) character.

Lines 39 to 43

This if statement determines if the demodesc system-descriptor area contains enough item descriptors for the parameterized SELECT statement. It compares the number of input parameters in the statement string (desc_count) with the number of item descriptors currently allocated (MAX_IDESC). If the program has not allocated enough item descriptors, the program deallocates the existing system-descriptor area (line 41) and allocates a new one (line 42); it uses the actual number of input parameters in the WITH MAX clause to specify the number of item descriptors to allocate.

Lines 44 and 45

This SET DESCRIPTOR statement stores the number of input parameters in the COUNT field of the demodesc system-descriptor area.

Lines 47 to 49

This SET DESCRIPTOR statement sets the TYPE, LENGTH (for a CHAR value), and DATA fields for each of the parameters in the WHERE clause. The program only calls SET DESCRIPTOR once because it assumes that the SELECT statement has only one input parameter. If you do not know the number of input parameters at compile time, put the SET DESCRIPTOR in a loop for which the desc_count host variable controls the number of iterations.

Lines 50 and 51

The database server executes the SELECT statement when it opens the democursor cursor. This OPEN statement includes the USING SQL DESCRIPTOR clause to specify the demodesc system-descriptor area as the location of the input-parameter values.

Lines 52 to 59

The program also uses the demodesc system-descriptor area to hold the columns that are returned by the SELECT statement. The DESCRIBE statement (line 53) examines the select list to determine the number and data types of these columns. The GET DESCRIPTOR statement (line 54) then obtains the number of described columns from the COUNT field of demodesc. Lines 55 to 58 then display the column information for each returned column. For more information on how to use a system-descriptor area to receive column values, see Handling an Unknown Select List.

Lines 60 to 70

These lines access the fields of the item descriptor for each column in the select list. After each FETCH statement, the GET DESCRIPTOR statement loads the contents of the DATA field into the result host variable.

Line 73

After all the rows are fetched, the CLOSE statement frees the resources allocated to the active set of the democursor cursor.

Lines 74 to 77

The FREE statement on line 74 frees the resources allocated to the demoid statement identifier while the FREE statement on line 75 frees the resources to the democursor cursor. The DEALLOCATE DESCRIPTOR statement frees the resources allocated to the demodesc system-descriptor area. For more information, see Freeing Memory Allocated to a System-Descriptor Area.

Executing a Parameterized Singleton SELECT

The instructions in the preceding section assume that the parameterized SELECT statement returns more than one row and, therefore, is associated with a cursor. If you know at the time you write the program that the parameterized SELECT statement will always return just one row, you can omit the cursor and use the EXECUTE...USING SQL DESCRIPTOR...INTO statement instead of the OPEN...USING SQL DESCRIPTOR statement to specify parameter values from a system-descriptor area. For more information, see Specifying Input Parameter Values.


INFORMIX-ESQL/C Programmer's Manual, Version 9.21
Copyright © 1999, Informix Software, Inc. All rights reserved