Specification: | CLI 1.1 | ODBC 1.0 | ISO CLI |
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().
SQLRETURN SQLBindCol (
SQLHSTMT StatementHandle, /* hstmt */
SQLUSMALLINT ColumnNumber, /* icol */
SQLSMALLINT TargetType, /* fCType */
SQLPOINTER TargetValuePtr, /* rgbValue */
SQLLEN BufferLength, /* dbValueMax */
SQLLEN *StrLen_or_IndPtr); /* *pcbValue */
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.
|
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:
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. |
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.
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.
The following sections describe how SQLBindCol() interacts with descriptors.
Argument mappings
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.
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.
|
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.
/* bind column 1 to variable */
cliRC = SQLBindCol(hstmt, 1, SQL_C_SHORT, &deptnumb.val, 0, &deptnumb.ind);