Using descriptors v17
Dynamic SQL allows a client application to execute SQL statements that are composed at runtime. This ability is useful when you don't know the content or form for a statement when you're writing a client application. ECPGPlus doesn't allow you to use a host variable in place of an identifier (such as a table name, column name, or index name). Instead, use dynamic SQL statements to build a string that includes the information, and then execute that string. The string is passed between the client and the server in the form of a descriptor. A descriptor is a data structure that contains both the data and the information about the shape of the data.
Overview of the client application flow
A client application must use a GET DESCRIPTOR
statement to retrieve information from a descriptor. The basic flow of a client application using dynamic SQL is:
- Use an
ALLOCATE DESCRIPTOR
statement to allocate a descriptor for the result set (select list). - Use an
ALLOCATE DESCRIPTOR
statement to allocate a descriptor for the input parameters (bind variables). - Obtain, assemble, or compute the text of an SQL statement.
- Use a
PREPARE
statement to parse and check the syntax of the SQL statement. - Use a
DESCRIBE
statement to describe the select list into the select-list descriptor. - Use a
DESCRIBE
statement to describe the input parameters into the bind-variables descriptor. - Prompt the user (if required) for a value for each input parameter. Use a
SET DESCRIPTOR
statement to assign the values into a descriptor. - Use a
DECLARE CURSOR
statement to define a cursor for the statement. - Use an
OPEN CURSOR
statement to open a cursor for the statement. - Use a
FETCH
statement to fetch each row from the cursor, storing each row in select-list descriptor. - Use a
GET DESCRIPTOR
command to interrogate the select-list descriptor to find the value of each column in the current row. - Use a
CLOSE CURSOR
statement to close the cursor and free any cursor resources.
Descriptor attributes
A descriptor can contain these attributes.
Field | Type | Attribute description |
---|---|---|
CARDINALITY | integer | The number of rows in the result set. |
DATA | N/A | The data value. |
DATETIME_INTERVAL_CODE | integer | If TYPE is 9 : 1 - DATE 2 - TIME 3 - TIMESTAMP 4 - TIME WITH TIMEZONE 5 - TIMESTAMP WITH TIMEZONE |
DATETIME_INTERVAL_PRECISION | integer | Unused. |
INDICATOR | integer | Indicates a NULL or truncated value. |
KEY_MEMBER | integer | Unused (returns FALSE ). |
LENGTH | integer | The data length (as stored on server). |
NAME | string | The name of the column in which the data resides. |
NULLABLE | integer | Unused (returns TRUE ). |
OCTET_LENGTH | integer | The data length (in bytes) as stored on server. |
PRECISION | integer | The data precision (if the data is of numeric type). |
RETURNED_LENGTH | integer | Actual length of data item. |
RETURNED_OCTET_LENGTH | integer | Actual length of data item. |
SCALE | integer | The data scale (if the data is of numeric type). |
TYPE | integer | A numeric code that represents the data type of the column: 1 - SQL3_CHARACTER 2 - SQL3_NUMERIC 3 - SQL3_DECIMAL 4 - SQL3_INTEGER 5 - SQL3_SMALLINT 6 - SQL3_FLOAT 7 - SQL3_REAL 8 - SQL3_DOUBLE_PRECISION 9 - SQL3_DATE_TIME_TIMESTAMP 10 - SQL3_INTERVAL 12 - SQL3_CHARACTER_VARYING 13 - SQL3_ENUMERATED 14 - SQL3_BIT 15 - SQL3_BIT_VARYING 16 - SQL3_BOOLEAN |
Example: Using a descriptor to return data
The following simple application executes an SQL statement entered by an end user. The code sample shows:
- How to use a SQL descriptor to execute a
SELECT
statement. - How to find the data and metadata returned by the statement.
The application accepts an SQL statement from an end user, tests the statement to see if it includes the SELECT
keyword, and executes the statement.
Using a SQL descriptor to execute a SELECT
statement
When invoking the application, an end user must provide the name of the database on which to perform the SQL statement and a string that contains the text of the query.
For example, a user might invoke the sample with the following command:
Sample Program:
The code sample begins by including the prototypes and type definitions for the C stdio
and stdlib
libraries, SQL data type symbols, and the SQLCA
(SQL communications area) structure:
The sample provides minimal error handling. When the application encounters a SQL error, it prints the error message to screen:
Finding the data and metadata returned by the statement
The application includes a forward-declaration for a function named print_meta_data()
that prints the metadata found in a descriptor:
The following code specifies the column header information that the application uses when printing the metadata:
The following declaration section identifies the host variables to contain the name of the database the application connects to, the content of the SQL statement, and a host variable for the number of columns in the result set (if any).
The application connects to the database using the default credentials:
Next, the application allocates a SQL descriptor to hold the metadata for a statement: