DB2 Version 9.7 for Linux, UNIX, and Windows

SQLBindCol function (CLI) - Bind a column to an application variable or LOB locator

Purpose

Specification: CLI 1.1 ODBC 1.0 ISO CLI
SQLBindCol() is used to associate (bind) columns in a result set to either:
  • Application variables or arrays of application variables (storage buffers), for all C data types. Data is transferred from the DBMS to the application when SQLFetch() or SQLFetchScroll() is called. Data conversion might occur as the data is transferred.
  • A LOB locator, for LOB columns. A LOB locator, not the data itself, is transferred from the DBMS to the application when SQLFetch() is called.

    Alternatively, LOB columns can be bound directly to a file using SQLBindFileToCol().

SQLBindCol() is called once for each column in the result set that the application needs to retrieve.

In general, SQLPrepare(), SQLExecDirect() or one of the schema functions is called before this function, and SQLFetch(), SQLFetchScroll(), SQLBulkOperations(), or SQLSetPos() is called after. Column attributes might also be needed before calling SQLBindCol(), and can be obtained using SQLDescribeCol() or SQLColAttribute().

Syntax

SQLRETURN   SQLBindCol (
               SQLHSTMT          StatementHandle,            /* hstmt */
               SQLUSMALLINT      ColumnNumber,               /* icol */
               SQLSMALLINT       TargetType,                 /* fCType */
               SQLPOINTER        TargetValuePtr,             /* rgbValue */
               SQLLEN            BufferLength,               /* dbValueMax */
               SQLLEN            *StrLen_or_IndPtr);         /* *pcbValue */

Function arguments

Table 1. SQLBindCol arguments
Data type Argument Use Description
SQLHSTMT StatementHandle input Statement handle
SQLUSMALLINT ColumnNumber input Number identifying the column. Columns are numbered sequentially, from left to right.
  • Column numbers start at 1 if bookmarks are not used (SQL_ATTR_USE_BOOKMARKS statement attribute set to SQL_UB_OFF).
  • Column numbers start at 0 if bookmarks are used (the statement attribute is set to SQL_UB_ON). Column 0 is the bookmark column.
SQLSMALLINT TargetType input The C data type for column number ColumnNumber in the result set. When the application retrieves data from the data source, it will convert the data to this C type. When using SQLBulkOperations() or SQLSetPos(), the driver will convert data from this C data type when sending information to the data source. The following types are supported:
  • SQL_C_BINARY
  • SQL_C_BIT
  • SQL_C_BLOB_LOCATOR
  • SQL_C_CHAR
  • SQL_C_CLOB_LOCATOR
  • SQL_C_DBCHAR
  • SQL_C_DBCLOB_LOCATOR
  • SQL_C_DECIMAL_IBM
  • SQL_C_DOUBLE
  • SQL_C_FLOAT
  • SQL_C_LONG
  • SQL_C_NUMERIC a
  • SQL_C_SBIGINT
  • SQL_C_SHORT
  • SQL_C_TYPE_DATE
  • SQL_C_TYPE_TIME
  • SQL_C_TYPE_TIMESTAMP
  • SQL_C_TYPE_TIMESTAMP_EXT
  • SQL_C_TINYINT
  • SQL_C_UBIGINT
  • SQL_C_UTINYINT
  • SQL_C_WCHAR

Specifying SQL_C_DEFAULT causes data to be transferred to its default C data type.

SQLPOINTER TargetValuePtr input/output (deferred) Pointer to buffer or an array of buffers with either column-wise or row-wise binding, where CLI is to store the column data or the LOB locator when the fetch occurs.

This buffer is used to return data when any of the following functions are called: SQLFetch(), SQLFetchScroll(), SQLSetPos() using the Operation argument SQL_REFRESH, or SQLBulkOperations() using the Operation argument SQL_FETCH_BY_BOOKMARK. Otherwise, SQLBulkOperations() and SQLSetPos() use the buffer to retrieve data.

If TargetValuePtr is null, the column is unbound. All columns can be unbound with a call to SQLFreeStmt() with the SQL_UNBIND option.

SQLLEN BufferLength input Size in bytes of TargetValuePtr buffer available to store the column data or the LOB locator.

If TargetType denotes a binary or character string (either single or double byte) or is SQL_C_DEFAULT for a column returning variable length data, then BufferLength must be > 0, or an error will be returned. Note that for character data, the driver counts the NULL termination character and so space must be allocated for it. For all other data types, this argument is ignored.

SQLLEN * StrLen_or_IndPtr input/output (deferred) Pointer to value (or array of values) which indicates the number of bytes CLI has available to return in the TargetValuePtr buffer. If TargetType is a LOB locator, the size of the locator is returned, not the size of the LOB data.

This buffer is used to return data when any of the following functions are called: SQLFetch(), SQLFetchScroll(), SQLSetPos() using the Operation argument SQL_REFRESH, or SQLBulkOperations() using the Operation argument SQL_FETCH_BY_BOOKMARK. Otherwise, SQLBulkOperations() and SQLSetPos() use the buffer to retrieve data.

SQLFetch() returns SQL_NULL_DATA in this argument if the data value of the column is null.

This pointer value must be unique for each bound column, or NULL.

A value of SQL_COLUMN_IGNORE, SQL_NTS, SQL_NULL_DATA, or the length of the data can be set for use with SQLBulkOperations().

SQL_NO_LENGTH might also be returned, refer to the Usage section below for more information.

Usage

Call SQLBindCol() once for each column in the result set for which either the data or, for LOB columns, the LOB locator is to be retrieved. When SQLFetch() or SQLFetchScroll() is called to retrieve data from the result set, the data in each of the bound columns is placed in the locations assigned by the TargetValuePtr and StrLen_or_IndPtr pointers. When the statement attribute SQL_ATTR_ROW_ARRAY_SIZE is greater than 1, then TargetType should refer to an array of buffers. If TargetType is a LOB locator, a locator value is returned, not the actual LOB data. The LOB locator references the entire data value in the LOB column.

If a CLI application does not provide an output buffer for a LOB column using the function SQLBindCol() the IBM® data server client will, by default, request a LOB locator on behalf of the application for each LOB column in the result sets.

Columns are identified by a number, assigned sequentially from left to right.
  • Column numbers start at 1 if bookmarks are not used (SQL_ATTR_USE_BOOKMARKS statement attribute set to SQL_UB_OFF).
  • Column numbers start at 0 if bookmarks are used (the statement attribute set to SQL_UB_ON).

After columns have been bound, in subsequent fetches the application can change the binding of these columns or bind previously unbound columns by calling SQLBindCol(). The new binding does not apply to data already fetched, it will be used on the next fetch. To unbind a single column (including columns bound with SQLBindFileToCol()), call SQLBindCol() with the TargetValuePtr pointer set to NULL. To unbind all the columns, the application should call SQLFreeStmt() with the Option input set to SQL_UNBIND.

The application must ensure enough storage is allocated for the data to be retrieved. If the buffer is to contain variable length data, the application must allocate as much storage as the maximum length of the bound column plus the NULL terminator. Otherwise, the data might be truncated. If the buffer is to contain fixed length data, CLI assumes the size of the buffer is the length of the C data type. If data conversion is specified, the required size might be affected.

If string truncation does occur, SQL_SUCCESS_WITH_INFO is returned and StrLen_or_IndPtr will be set to the actual size of TargetValuePtr available for return to the application.

Truncation is also affected by the SQL_ATTR_MAX_LENGTH statement attribute (used to limit the amount of data returned to the application). The application can specify not to report truncation by calling SQLSetStmtAttr() with SQL_ATTR_MAX_LENGTH and a value for the maximum length to return for all variable length columns, and by allocating a TargetValuePtr buffer of the same size (plus the null-terminator). If the column data is larger than the set maximum length, SQL_SUCCESS will be returned when the value is fetched and the maximum length, not the actual length, will be returned in StrLen_or_IndPtr.

If the column to be bound is a SQL_GRAPHIC, SQL_VARGRAPHIC or SQL_LONGVARGRAPHIC type, then TargetType can be set to SQL_C_DBCHAR or SQL_C_CHAR. If TargetType is SQL_C_DBCHAR, the data fetched into the TargetValuePtr buffer will be null-terminated with a double byte null-terminator. If TargetType is SQL_C_CHAR, then there will be no null-termination of the data. In both cases, the length of the TargetValuePtr buffer (BufferLength) is in units of bytes and should therefore be a multiple of 2. It is also possible to force CLI to null terminate graphic strings using the PATCH1 keyword.

Note: SQL_NO_TOTAL will be returned in StrLen_or_IndPtr if:

Descriptors and SQLBindCol

The following sections describe how SQLBindCol() interacts with descriptors.

Note: Calling SQLBindCol() for one statement can affect other statements. This occurs when the ARD associated with the statement is explicitly allocated and is also associated with other statements. Because SQLBindCol() modifies the descriptor, the modifications apply to all statements with which this descriptor is associated. If this is not the required behavior, the application should dissociate this descriptor from the other statements before calling SQLBindCol().

Argument mappings

Conceptually, SQLBindCol() performs the following steps in sequence:
  • Calls SQLGetStmtAttr() to obtain the ARD handle.
  • Calls SQLGetDescField() to get this descriptor's SQL_DESC_COUNT field, and if the value in the ColumnNumber argument exceeds the value of SQL_DESC_COUNT, calls SQLSetDescField() to increase the value of SQL_DESC_COUNT to ColumnNumber.
  • Calls SQLSetDescField() multiple times to assign values to the following fields of the ARD:
    • Sets SQL_DESC_TYPE and SQL_DESC_CONCISE_TYPE to the value of TargetType.
    • Sets one or more of SQL_DESC_LENGTH, SQL_DESC_PRECISION, SQL_DESC_SCALE as appropriate for TargetType.
    • Sets the SQL_DESC_OCTET_LENGTH field to the value of BufferLength.
    • Sets the SQL_DESC_DATA_PTR field to the value of TargetValue.
    • Sets the SQL_DESC_INDICATOR_PTR field to the value of StrLen_or_IndPtr (see the following paragraph).
    • Sets the SQL_DESC_OCTET_LENGTH_PTR field to the value of StrLen_or_IndPtr (see the following paragraph).
The variable that the StrLen_or_IndPtr argument refers to is used for both indicator and length information. If a fetch encounters a null value for the column, it stores SQL_NULL_DATA in this variable; otherwise, it stores the data length in this variable. Passing a null pointer as StrLen_or_IndPtr keeps the fetch operation from returning the data length, but makes the fetch fail if it encounters a null value and has no way to return SQL_NULL_DATA.

If the call to SQLBindCol() fails, the content of the descriptor fields it would have set in the ARD are undefined, and the value of the SQL_DESC_COUNT field of the ARD is unchanged.

Implicit resetting of COUNT field

SQLBindCol() sets SQL_DESC_COUNT to the value of the ColumnNumber argument only when this would increase the value of SQL_DESC_COUNT. If the value in the TargetValuePtr argument is a null pointer and the value in the ColumnNumber argument is equal to SQL_DESC_COUNT (that is, when unbinding the highest bound column), then SQL_DESC_COUNT is set to the number of the highest remaining bound column.

Cautions regarding SQL_C_DEFAULT

To retrieve column data successfully, the application must determine correctly the length and starting point of the data in the application buffer. When the application specifies an explicit TargetType, application misconceptions are readily detected. However, when the application specifies a TargetType of SQL_C_DEFAULT, SQLBindCol() can be applied to a column of a different data type from the one intended by the application, either from changes to the metadata or by applying the code to a different column. In this case, the application might fail to determine the start or length of the fetched column data. This can lead to unreported data errors or memory violations.

Return codes

  • SQL_SUCCESS
  • SQL_SUCCESS_WITH_INFO
  • SQL_ERROR
  • SQL_INVALID_HANDLE

Diagnostics

Table 2. SQLBindCol SQLSTATEs
SQLSTATE Description Explanation
07009 Invalid descriptor index The value specified for the argument ColumnNumber exceeded the maximum number of columns in the result set, or the value specified was less than 0.
40003 08S01 Communication link failure. The communication link between the application and data source failed before the function completed.
58004 Unexpected system failure. Unrecoverable system error.
HY001 Memory allocation failure. DB2® CLI is unable to allocate memory required to support execution or completion of the function. It is likely that process-level memory has been exhausted for the application process. Consult the operating system configuration for information on process-level memory limitations.
HY003 Program type out of range. TargetType was not a valid data type or SQL_C_DEFAULT.
HY010 Function sequence error. The function was called while in a data-at-execute (SQLParamData(), SQLPutData()) operation.

The function was called while within a BEGIN COMPOUND and END COMPOUND SQL operation.

HY013 Unexpected memory handling error. DB2 CLI was unable to access memory required to support execution or completion of the function.
HY090 Invalid string or buffer length. The value specified for the argument BufferLength is less than 1 and the argument TargetType is either SQL_C_CHAR, SQL_C_BINARY or SQL_C_DEFAULT.
HYC00 Driver not capable. CLI recognizes, but does not support the data type specified in the argument TargetType

A LOB locator C data type was specified, but the connected server does not support LOB data types.

Note: Additional diagnostic messages relating to the bound columns might be reported at fetch time.

Restrictions

The LOB data support is only available when connected to a server that supports large object data types. If the application attempts to specify a LOB locator C data type for a server that does not support it, SQLSTATE HYC00 will be returned.

Example

 /* bind column 1 to variable */
 cliRC = SQLBindCol(hstmt, 1, SQL_C_SHORT, &deptnumb.val, 0, &deptnumb.ind);