DB2 Version 9.7 for Linux, UNIX, and Windows

SQLGetData function (CLI) - Get data from a column

Purpose

Specification: CLI 1.1 ODBC 1.0 ISO CLI

SQLGetData() retrieves data for a single column in the current row of the result set. This is an alternative to SQLBindCol(), which is used to transfer data directly into application variables or LOB locators on each SQLFetch() or SQLFetchScroll() call. An application can either bind LOBs with SQLBindCol() or use SQLGetData() to retrieve LOBs, but both methods cannot be used together. SQLGetData() can also be used to retrieve large data values in pieces.

SQLFetch() or SQLFetchScroll() must be called before SQLGetData().

After calling SQLGetData() for each column, SQLFetch() or SQLFetchScroll() is called to retrieve the next row.

Syntax

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

Function arguments

Table 1. SQLGetData arguments
Data type Argument Use Description
SQLHSTMT StatementHandle input Statement handle
SQLUSMALLINT ColumnNumber input Column number for which the data retrieval is requested. Result set 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 set to SQL_UB_ON or SQL_UB_VARIABLE).
SQLSMALLINT TargetType input The C data type of the column identifier by ColumnNumber. 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_ARD_TYPE results in the data being converted to the data type specified in the SQL_DESC_CONCISE_TYPE field of the ARD.

Specifying SQL_C_DEFAULT results in the data being converted to its default C data type.

SQLPOINTER TargetValuePtr output Pointer to buffer where the retrieved column data is to be stored.
SQLLEN BufferLength input Maximum size of the buffer pointed to by TargetValuePtr. This value is ignored when the driver returns fixed-length data.
SQLLEN * StrLen_or_IndPtr output Pointer to value which indicates the number of bytes CLI has available to return in the TargetValuePtr buffer. If the data is being retrieved in pieces, this contains the number of bytes still remaining.

The value is SQL_NULL_DATA if the data value of the column is null. If this pointer is NULL and SQLFetch() has obtained a column containing null data, then this function will fail because it has no means of reporting this.

If SQLFetch() has fetched a column containing binary data, then the pointer to StrLen_or_IndPtr must not be NULL or this function will fail because it has no other means of informing the application about the length of the data retrieved in the TargetValuePtr buffer.

Note: CLI will provide some performance enhancement if TargetValuePtr is placed consecutively in memory after StrLen_or_IndPtr

Usage

Different DB2® data sources have different restrictions on how SQLGetData() can be used. For an application to be sure about the functional capabilities of this function, it should call SQLGetInfo() with any of the following SQL_GETDATA_EXTENSIONS options:
  • SQL_GD_ANY_COLUMN: If this option is returned, SQLGetData() can be called for any unbound column, including those before the last bound column. All DB2 data sources support this feature.
  • SQL_GD_ANY_ORDER: If this option is returned, SQLGetData() can be called for unbound columns in any order. All DB2 data sources support this feature.
  • SQL_GD_BLOCK: If this option if returned by SQLGetInfo() for the SQL_GETDATA_EXTENSIONS InfoType argument, then the driver will support calls to SQLGetData() when the rowset size is greater than 1. The application can also call SQLSetPos() with the SQL_POSITION option to position the cursor on the correct row before calling SQLGetData(). At least DB2 for UNIX and Windows data sources support this feature.
  • SQL_GD_BOUND: If this option is returned, SQLGetData() can be called for bound columns as well as unbound columns. DB2 for Linux, UNIX, and Windows does not currently support this feature.

SQLGetData() can also be used to retrieve long columns if the C data type (TargetType) is SQL_C_CHAR, SQL_C_BINARY, SQL_C_DBCHAR, SQL_C_WCHAR, or if TargetType is SQL_C_DEFAULT and the column type denotes a binary or character string.

Upon each SQLGetData() call, if the data available for return is greater than or equal to BufferLength, truncation occurs. Truncation is indicated by a function return code of SQL_SUCCESS_WITH_INFO coupled with a SQLSTATE denoting data truncation. The application can call SQLGetData() again, with the same ColumnNumber value, to get subsequent data from the same unbound column starting at the point of truncation. To obtain the entire column, the application repeats such calls until the function returns SQL_SUCCESS. The next call to SQLGetData() returns SQL_NO_DATA_FOUND.

When the application calls the function SQLGetData() to retrieve the actual LOB data it will, by default, make one request to the server and will store the entire LOB in memory provided BufferLength is large enough. If BufferLength is not large enough to hold the requested LOB data, it will be retrieved in pieces.

Although SQLGetData() can be used for the sequential retrieval of LOB column data, use the CLI LOB functions if only a portion of the LOB data or a few sections of the LOB column data are needed:
  1. Bind the column to a LOB locator.
  2. Fetch the row.
  3. Use the locator in a SQLGetSubString() call, to retrieve the data in pieces (SQLGetLength() and SQLGetPosition() might also be required in order to determine the values of some of the arguments).
  4. Repeat step 2.

Truncation is also affected by the SQL_ATTR_MAX_LENGTH statement attribute. The application can specify that truncation is not to be reported by calling SQLSetStmtAttr() with SQL_ATTR_MAX_LENGTH and a value for the maximum length to return for any one column, 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 and the maximum length, not the actual length will be returned in StrLen_or_IndPtr.

To discard the column data part way through the retrieval, the application can call SQLGetData() with ColumnNumber set to the next column position of interest. To discard data that has not been retrieved for the entire row, the application should call SQLFetch() to advance the cursor to the next row; or, if it does not want any more data from the result set, the application can close the cursor by calling SQLCloseCursor() or SQLFreeStmt() with the SQL_CLOSE or SQL_DROP option.

The TargetType input argument determines the type of data conversion (if any) needed before the column data is placed into the storage area pointed to by TargetValuePtr.

For SQL graphic column data:
  • The length of the TargetValuePtr buffer (BufferLength) should be a multiple of 2. The application can determine the SQL data type of the column by first calling SQLDescribeCol() or SQLColAttribute().
  • The pointer to StrLen_or_IndPtr must not be NULL since CLI will be storing the number of octets stored in TargetValuePtr.
  • If the data is to be retrieved in piecewise fashion, CLI will attempt to fill TargetValuePtr to the nearest multiple of two octets that is still less than or equal to BufferLength. This means if BufferLength is not a multiple of two, the last byte in that buffer will be untouched; CLI will not split a double-byte character.

The content returned in TargetValuePtr is always null-terminated unless the column data to be retrieved is binary, or if the SQL data type of the column is graphic (DBCS) and the C buffer type is SQL_C_CHAR. If the application is retrieving the data in multiple chunks, it should make the proper adjustments (for example, strip off the null-terminator before concatenating the pieces back together assuming the null termination environment attribute is in effect).

Truncation of numeric data types is reported as a warning if the truncation involves digits to the right of the decimal point. If truncation occurs to the left of the decimal point, an error is returned (refer to the diagnostics section).

With the exception of scrollable cursors, applications that use SQLFetchScroll() to retrieve data should call SQLGetData() only when the rowset size is 1 (equivalent to issuing SQLFetch()). SQLGetData() can only retrieve column data for a row where the cursor is currently positioned.

Using SQLGetData() with Scrollable Cursors

SQLGetData() can also be used with scrollable cursors. You can save a pointer to any row in the result set with a bookmark. The application can then use that bookmark as a relative position to retrieve a rowset of information.

Once you have positioned the cursor to a row in a rowset using SQLSetPos(), you can obtain the bookmark value from column 0 using SQLGetData(). In most cases you will not want to bind column 0 and retrieve the bookmark value for every row, but use SQLGetData() to retrieve the bookmark value for the specific row you require.

Return codes

SQL_NO_DATA_FOUND is returned when the preceding SQLGetData() call has retrieved all of the data for this column.

SQL_SUCCESS is returned if a zero-length string is retrieved by SQLGetData(). If this is the case, StrLen_or_IndPtr will contain 0, and TargetValuePtr will contain a null terminator.

SQL_NO_TOTAL is returned as the length when truncation occurs if the CLI configuration keyword StreamGetData is set to 1 and CLI cannot determine the number of bytes still available to return in the output buffer.

If the preceding call to SQLFetch() failed, SQLGetData() should not be called since the result is undefined.

Diagnostics

Table 2. SQLGetData SQLSTATEs
SQLSTATE Description Explanation
01004 Data truncated. Data returned for the specified column (ColumnNumber) was truncated. String or numeric values are right truncated. SQL_SUCCESS_WITH_INFO is returned.
07006 Invalid conversion. The data value cannot be converted to the C data type specified by the argument TargetType.

The function has been called before for the same ColumnNumber value but with a different TargetType value.

07009 Invalid descriptor index. The value specified for ColumnNumber was equal to 0, and the SQL_ATTR_USE_BOOKMARKS statement attribute was SQL_UB_OFF. The value specified for the argument ColumnNumber was greater than the number of columns in the result set.
22002 Invalid output or indicator buffer specified. The pointer value specified for the argument StrLen_or_IndPtr was a null pointer and the value of the column is null. There is no means to report SQL_NULL_DATA.
22003 Numeric value out of range. Returning the numeric value (as numeric or string) for the column would have caused the whole part of the number to be truncated.
22005 Error in assignment. A returned value was incompatible with the data type denoted by the argument TargetType.
22007 Invalid datetime format. Conversion from character a string to a datetime format was indicated, but an invalid string representation or value was specified, or the value was an invalid date.
22008 Datetime field overflow. Datetime field overflow occurred; for example, an arithmetic operation on a date or timestamp has a result that is not within the valid range of dates, or a datetime value cannot be assigned to a bound variable because it is too small.
24000 Invalid cursor state. The previous SQLFetch() resulted in SQL_ERROR or SQL_NO_DATA found; as a result, the cursor is not positioned on a row.
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 specified StatementHandle was not in a cursor positioned state. The function was called without first calling SQLFetch().

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.

An asynchronously executing function (not this one) was called For the StatementHandle and was still executing when this function was called.

The function was called before a statement was prepared on the statement handle.

HY011 Operation invalid at this time. Calls to SQLGetData() for previously accessed LOB columns are not allowed. Refer to AllowGetDataLOBReaccess CLI/ODBC configuration keyword for more information.
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 of the argument BufferLength is less than 0 and the argument TargetType is SQL_C_CHAR, SQL_C_BINARY, SQL_C_DBCHAR or (SQL_C_DEFAULT and the default type is one of SQL_C_CHAR, SQL_C_BINARY, or SQL_C_DBCHAR).
HYC00 Driver not capable. The SQL data type for the specified data type is recognized but not supported by CLI.

The requested conversion from the SQL data type to the application data TargetType cannot be performed by CLI or the data source.

The column was bound using SQLBindFileToCol().

HYT00 Timeout expired. The timeout period expired before the data source returned the result set. The timeout period can be set using the SQL_ATTR_QUERY_TIMEOUT attribute for SQLSetStmtAttr().

Restrictions

None.

Example

    /* use SQLGetData to get the results */
    /* get data from column 1 */
    cliRC = SQLGetData(hstmt,
                       1,
                       SQL_C_SHORT,
                       &deptnumb.val,
                       0,
                       &deptnumb.ind);
    STMT_HANDLE_CHECK(hstmt, hdbc, cliRC);

    /* get data from column 2 */
    cliRC = SQLGetData(hstmt,
                       2,
                       SQL_C_CHAR,
                       location.val,
                       15,
                       &location.ind);