SQLBindCol() - Bind a column to an application variable

SQLBindCol() binds a column to an application variable. You can call SQLBindCol() once for each column in a result set from which you want to retrieve data or LOB locators.

ODBC specifications for SQLBindCol()

Table 1. SQLBindCol() specifications
ODBC specification level In X/Open CLI CAE specification? In ISO CLI specification?
1.0 Yes Yes

Syntax

For 31-bit applications, use the following syntax:

SQLRETURN   SQLBindCol       (SQLHSTMT          hstmt,
                              SQLUSMALLINT      icol,
                              SQLSMALLINT       fCType,
                              SQLPOINTER        rgbValue,
                              SQLINTEGER        cbValueMax,
                              SQLINTEGER FAR   *pcbValue);

For 64-bit applications, use the following syntax:

SQLRETURN   SQLBindCol       (SQLHSTMT          hstmt,
                              SQLUSMALLINT      icol,
                              SQLSMALLINT       fCType,
                              SQLPOINTER        rgbValue,
                              SQLLEN            cbValueMax,
                              SQLLEN     FAR   *pcbValue);

Function arguments

The following table lists the data type, use, and description for each argument in this function.

Table 2. SQLBindCol() arguments
Data type Argument Use Description
SQLHSTMT hstmt input Specifies the statement handle on which results are returned.
SQLUSMALLINT icol input Specifies the number that identifies the column you bind. Columns are numbered sequentially, from left to right, starting at 1.
SQLSMALLINT fCType input The C data type for column number icol in the result set. The following types are supported:
  • SQL_C_BIGINT
  • SQL_C_BINARY
  • SQL_C_BINARYXML
  • SQL_C_BIT
  • SQL_C_BLOB_LOCATOR
  • SQL_C_CHAR
  • SQL_C_CLOB_LOCATOR
  • SQL_C_DBCHAR
  • SQL_C_DBCLOB_LOCATOR
  • SQL_C_DECIMAL64
  • SQL_C_DECIMAL128
  • SQL_C_DOUBLE
  • SQL_C_FLOAT
  • SQL_C_LONG
  • SQL_C_SHORT
  • SQL_C_TYPE_DATE
  • SQL_C_TYPE_TIME
  • SQL_C_TYPE_TIMESTAMP
  • SQL_C_TYPE_TIMESTAMP_EXT
  • SQL_C_TYPE_TIMESTAMP_EXT_TZ
  • SQL_C_TINYINT
  • SQL_C_WCHAR
The supported data types are based on the data source to which you are connected. Specifying SQL_C_DEFAULT causes data to be transferred to its default C data type.
SQLPOINTER rgbValue output (deferred) Points to a buffer (or an array of buffers when you use the SQLExtendedFetch() function) where Db2 ODBC stores the column data or the LOB locator when you fetch data from the bound column.

If the rgbValue argument is null, the column is unbound.

SQLINTEGER (31-bit) or SQLLEN (64-bit) 1 cbValueMax input Specifies the size of the rgbValue buffer in bytes that are available to store the column data or the LOB locator.

If the fCType argument denotes a binary or character string (either single-byte or double-byte) or is SQL_C_DEFAULT, cbValueMax must be greater than 0, or an error occurs. If the fCType argument denotes other data types, the cbValueMax argument is ignored.

SQLINTEGER * (31-bit) or SQLLEN * (64-bit) 1 pcbValue output (deferred) Pointer to a value (or array of values) that indicates the number of bytes that Db2 ODBC has available to return in the rgbValue buffer. If fCType is a LOB locator, the size of the locator, not the size of the LOB data, is returned.

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

This pointer value can be null. If this pointer is not null, it must be unique for each bound column.

SQL_NO_LENGTH can also be returned. See Usage for more details.

Notes:
  1. For 64-bit applications, the data type SQLINTEGER, which was used in previous versions of Db2, is still valid. However, for maximum application portability, using SQLLEN is recommended.

Important: You must ensure the locations that the pointers rgbValue and pcbValue reference are valid until you call SQLFetch() or SQLExtendedFetch(). For SQLBindCol(), the pointers rgbValue and pcbValue are deferred outputs, which means that the storage locations to which they point are not updated until you fetch a row from the result set. For example, if you call SQLBindCol() within a local function, you must call SQLFetch() from within the same scope of the function, or you must allocate the rgbValue buffer as static or global.

Tip: Place the buffer that the rgbValue argument specifies consecutively in memory after the buffer that the pcbValue argument specifies for better Db2 ODBC performance for all varying-length data types. See Usage for more details.

Usage

Use SQLBindCol() to associate, or bind, columns in a result set with the following elements of your application:
  • Application variables or arrays of application variables (storage buffers) for all C data types. When you bind columns to application variables, data is transferred from the database management system to the application when you call SQLFetch() or SQLExtendedFetch(). This transfer converts data from an SQL type to any supported C type variable that you specify in the SQLBindCol() call.
  • A LOB locator, for LOB columns. When you bind to LOB locators, the locator, not the data itself, is transferred from the database management system to the application when you call SQLFetch(). A LOB locator can represent the entire data or a portion of the data.

Call SQLBindCol() once for each column in a result set from which you want to retrieve data or LOB locators. You generate result sets when you call SQLPrepare(), SQLExecDirect(), SQLGetTypeInfo(), or one of the catalog functions. After you bind columns to a result set, call SQLFetch() to place data from these columns into application storage locations (the locations to which the rgbValue and cbValue arguments point). If the fCType argument specifies a LOB locator, a locator value (not the LOB data itself) is placed in these locations. This locator value references the entire data value in the LOB column at the server. You might need to obtain column attributes before you call SQLBindCol(). To obtain these attributes, call SQLDescribeCol() or SQLColAttribute().

You can use SQLExtendedFetch() in place of SQLFetch() to retrieve multiple rows from the result set into an array. In this case, the rgbValue argument references an array. You cannot mix calls to SQLExtendedFetch() with calls to SQLFetch() on the same result set.

Obtaining information about the result set: Columns are identified by a number, assigned sequentially from left to right, starting at 1. To determine the number of columns in a result set, call SQLNumResultCols(), or call SQLColAttribute() with the FieldIdentifier argument set to SQL_DESC_COUNT.

Call SQLDescribeCol() or SQLColAttribute() to query the attributes (such as data type and length) of a column. You can then use this information to allocate a storage location with a C data type and length that match the SQL data type an length of the result set column. In the case of LOB data types, you can retrieve a locator instead of the entire LOB value.

You can choose which, if any, columns that you want to bind from a result set. For unbound columns, use SQLGetData() instead of, or in conjunction with, SQLBindCol() to retrieve data. Generally, SQLBindCol() is more efficient than SQLGetData().

During subsequent fetches, you can use SQLBindCol() to change which columns are bound to application variables or to bind previously unbound columns. New binds do not apply to data that you have fetched; these binds are used for the next fetch. To unbind a single column, call SQLBindCol() with the rgbValue pointer set to NULL. To unbind all the columns, call SQLFreeStmt() with the fOption input set to SQL_UNBIND.

Allocating buffers: Ensure that you allocate enough storage to hold the data that you retrieve. When you allocate a buffer to hold varying-length data, allocate an amount of storage that is equal to the maximum length of data that the column that is bound to this buffer can produce. If you allocate less storage than this maximum, SQLFetch() or SQLExtendedFetch() truncates any data that is larger than the space that you allocated. When you allocate a buffer that holds fixed-length data, Db2 ODBC assumes that the size of the buffer is the length of the C data type. If you specify data conversion, the amount of space that the data requires might change.

When you bind a column that is defined as SQL_GRAPHIC, SQL_VARGRAPHIC, or SQL_LONGVARGRAPHIC, you can set the fCType argument to SQL_C_DBCHAR, SQL_C_WCHAR, or SQL_C_CHAR. If you set the fCType argument to SQL_C_DBCHAR or SQL_C_WCHAR, the data that you fetch into the rgbValue buffer is nul-terminated by a double-byte nul-terminator. If you set the fCType argument to SQL_C_CHAR, the data that you fetch is not always nul-terminated. In both cases, the length of the rgbValue buffer (cbValueMax) is in units of bytes, and the value is always a multiple of 2.

When you bind a varying-length column, Db2 ODBC can write to both of the buffers that specified by the pcbValue and rgbValue arguments in one operation if you allocate these buffers contiguously. The following example illustrates how to allocate these buffers contiguously:

struct {  SQLINTEGER  pcbValue;
          SQLCHAR     rgbValue[MAX_BUFFER];
       } column;
When the pcbValue and rgbValue arguments are contiguous, SQL_NO_TOTAL is returned in the pcbValue argument if your bind meets all of the following conditions:
  • The SQL type is a varying-length type.
  • The column type is NOT NULLABLE.
  • String truncation occurred.

Handling data truncation: If SQLFetch() or SQLExtendedFetch() truncates data, it returns SQL_SUCCESS_WITH_INFO and set the pcbValue argument to a value that represents the amount of space (in bytes) that the full data requires.

Truncation is also affected by the SQL_ATTR_MAX_LENGTH statement attribute (which is used to limit the amount of data that your application returns). You can disable truncation warnings with the following procedure:
  1. Call SQLSetStmtAttr().
    • Set the Attribute argument to SQL_ATTR_MAX_LENGTH.
    • Point the ValuePtr argument to a buffer that contains the value for the maximum length, in bytes, of varying-length columns that you want to receive.
  2. Allocate the rgbValue argument on your SQLBindCol() call as a buffer that is the same size (plus the nul-terminator) as you set for the value of the SQL_ATTR_MAX_LENGTH statement attribute.
If the column data is larger than the maximum length that you specified, the maximum length, not the actual length, is returned in the buffer to which the pcbValue argument points. If data is truncated because it exceeds the maximum length that the SQL_ATTR_MAX_LENGTH statement attribute specifies, you receive no warning of this truncation. SQLFetch() and SQLExtendedFetch() return SQL_SUCCESS for data that is truncated in this way.

When you bind a column that holds SQL_ROWID data, you can set the fCType argument to SQL_C_CHAR or SQL_C_DEFAULT. The data that you fetch into the buffer that the rgbValue argument specifies is nul-terminated. The maximum length of a ROWID column in the database management system is 40 bytes. Therefore, to retrieve this type of data without truncation, you must allocate an rgbValue buffer of at least 40 bytes in your application.

Handling encoding schemes: The Db2 ODBC driver determines one of the following encoding schemes for character and graphic data through the settings of the CURRENTAPPENSCH keyword (which appears in the initialization file) and the fCType argument (which you specify in SQLBindCol() calls).
  • The ODBC driver places EBCDIC data into application variables when both of the following conditions are true:
    • CURRENTAPPENSCH = EBCDIC is specified in the initialization file, the CCSID that is specified for the CURRENTAPPENSCH keyword is an EBCDIC CCSID, or the CURRENTAPPENSCH keyword is not specified in the initialization file.
    • The fCType argument specifies SQL_C_CHAR or SQL_C_DBCHAR in the SQLBindCol() call.
  • The ODBC driver places Unicode UCS-2 data into application variables when the fCType argument specifies SQL_C_WCHAR in the SQLBindCol() call.
  • The ODBC driver places Unicode UTF-8 data into application variables when both of the following conditions are true:
    • CURRENTAPPENSCH = UNICODE is specified in the initialization file, or the CCSID that is specified for the CURRENTAPPENSCH keyword is a Unicode CCSID (1200, 1208, 13488 or 17584).
    • The fCType argument specifies SQL_C_CHAR in the SQLBindCol() call.
  • The ODBC driver places ASCII data into application variables when both of the following conditions are true:
    • CURRENTAPPENSCH = ASCII is specified in the initialization file, or the CCSID that is specified for the CURRENTAPPENSCH keyword is an ASCII CCSID.
    • The fCType argument specifies SQL_C_CHAR or SQL_C_DBCHAR in the SQLBindCol() call.

Retrieved UTF-8 data is terminated by a single-byte nul-terminator, where as retrieved UCS-2 data is terminated by a double-byte nul-terminator.

Binding LOB columns: You generally treat LOB locators like any other data type, but when you use LOB locators the following differences apply:
  • The server generates locator values when you fetch from a column that is bound to the LOB locator C data type and passes only the locator, not the data, to the application.
  • When you call SQLGetSubString() to define a locator on a portion of another LOB, the server generates a new locator and transfers only the locator to the application.
  • The value of a locator is valid only within the current transaction. You cannot store a locator value and use it beyond the current transaction, even if you specify the WITH HOLD attribute when you define the cursor that you use to fetch the locator.
  • You can use the FREE LOCATOR statement to free a locator before the end of a transaction.
  • When your application receives a locator, you can use SQLGetSubString() to either receive a portion of the LOB value or to generate another locator that represents a portion of the LOB value. You can also use locator values as input for a parameter marker (with the SQLBindParameter() function).

    A LOB locator is not a pointer to a database position; rather, it is a reference to a LOB value, a snapshot of that LOB value. The current position of the cursor and the row from which the LOB value is extracted are not associated. Therefore, even after the cursor moves to a different row, the LOB locator (and thus the value that it represents) can still be referenced.

  • With locators, you can use SQLGetPosition() and SQLGetLength() with SQLGetSubString() to define a substring of a LOB value.
You can bind a LOB column to one of the following data types:
  • A storage buffer (to hold the entire LOB data value)
  • A LOB locator (to hold the locator value only)

The most recent bind column function call determines the type of binding that is in effect.

Return codes

After you call SQLBindCol(), it returns one of the following values:
  • SQL_SUCCESS
  • SQL_ERROR
  • SQL_INVALID_HANDLE

Diagnostics

The following table lists each SQLSTATE that this function generates, with a description and explanation for each value.

Table 3. SQLBindCol() SQLSTATEs
SQLSTATE Description Explanation
08S01 Communication link failure. The communication link between the application and data source fails before the function completes.
58004 Unexpected system failure. Unrecoverable system error.
HY001 Memory allocation failure. Db2 ODBC is not able to allocate the required memory to support the execution or the completion of the function.
HY002 Invalid column number. The specified value for the icol argument is less than 0 or greater than the number of columns in the result set.
HY003 Program type out of range. The fCType argument is not a valid data type or SQL_C_DEFAULT.
HY010 Function sequence error. The function is called during a data-at-execute operation. (That is, the function is called during a procedure that uses the SQLParamData() or SQLPutData() functions.)
HY013 Unexpected memory handling error. Db2 ODBC is not able to allocate the required memory to support the execution or the completion of the function. Db2 ODBC is not able to access the memory that is required to support execution or completion of the function.
HY090 Invalid string or buffer length. The specified value for the cbValueMax argument is less than 0.
HYC00 Driver not capable. Db2 ODBC does not support the value that the fCType argument specifies.

Important: Additional diagnostic messages that relate to the bound columns might be reported at fetch time.

Example

Refer to SQLExtendedFetch() for more details. This function returns a block of data containing multiple rows for each bound column.