SQLSpecialColumns - Get special (row identifier) columns

Purpose

SQLSpecialColumns() returns unique row identifier information (primary key or unique index) for a table. For example, unique index or primary key information. The information is returned in an SQL result set, which can be retrieved using the same functions that are used to fetch a result set generated by a SELECT-statement.

Syntax

SQLRETURN SQLSpecialColumns  (SQLHSTMT       hstmt,
                              SQLSMALLINT    fColType,
                              SQLCHAR        *szCatalogName,
                              SQLSMALLINT    cbCatalogName,
                              SQLCHAR        *szSchemaName,
                              SQLSMALLINT    cbSchemaName,
                              SQLCHAR        *szTableName,
                              SQLSMALLINT    cbTableName,
                              SQLSMALLINT    fScope,
                              SQLSMALLINT    fNullable);

Function arguments

Table 1. SQLSpecialColumns arguments
Data type Argument Use Description
SQLCHAR * szCatalogName Input Catalog qualifier of a three-part table name. This must be a null pointer or a zero length string.
SQLCHAR * szSchemaName Input Schema qualifier of the specified table.
SQLCHAR * szTableName Input Table name.
SQLHSTMT hstmt Input Statement handle.
SQLSMALLINT cbCatalogName Input Length of szCatalogName. This must be a set to 0.
SQLSMALLINT cbSchemaName Input Length of szSchemaName.
SQLSMALLINT cbTableName Input Length of cbTableName.
SQLSMALLINT fColType Input Reserved for future use to support additional types of special columns.

This data type is currently ignored.

SQLSMALLINT fNullable Input Determines whether to return special columns that can have a NULL value.
Must be one of the following values:
  • SQL_NO_NULLS

    The row identifier column set returned cannot have any NULL values.

  • SQL_NULLABLE

    The row identifier column set returned can include columns where NULL values are permitted.

SQLSMALLINT fScope Input Minimum required duration for which the unique row identifier is valid.
fScope must be one of the following values:
  • SQL_SCOPE_CURROW - The row identifier is guaranteed to be valid only while positioned on that row. A later reselect using the same row identifier values might not return a row if the row is updated or deleted by another transaction.
  • SQL_SCOPE_TRANSACTION - The row identifier is guaranteed to be valid for the duration of the current transaction.
  • SQL_SCOPE_SESSION - The row identifier is guaranteed to be valid for the duration of the connection.
The duration over which a row identifier value is guaranteed to be valid depends on the current transaction isolation level. For information and scenarios involving isolation levels, refer to the IBM® DB2® SQL Reference.

Usage

If multiple ways exist to uniquely identify any row in a table (for example, if there are multiple unique indexes on the specified table), then DB2 UDB CLI returns the best set of row identifier columns based on its internal criterion.

If there is no column set that allows any row in the table to be uniquely identified, an empty result set is returned.

The unique row identifier information is returned in the form of a result set where each column of the row identifier is represented by one row in the result set. The result set returned by SQLSpecialColumns() has the following columns in the following order:
Table 2. Columns returned by SQLSpecialColumns
Column name Data type Description
SCOPE SMALLINT not NULL Actual scope of the rowid. Contains one of the following values:
  • SQL_SCOPE_CURROW
  • SQL_SCOPE_TRANSACTION
  • SQL_SCOPE_SESSION

Refer to fScope in Table 1 for a description of each value.

COLUMN_NAME VARCHAR(128) not NULL Name of the row identifier column.
DATA_TYPE SMALLINT not NULL SQL data type of the column.
TYPE_NAME VARCHAR(128) not NULL Database Management System (DBMS) character string represented of the name associated with DATA_TYPE column value.
LENGTH_PRECISION INTEGER The precision of the column. NULL is returned for data types where precision is not applicable.
BUFFER_LENGTH INTEGER The length, in bytes, of the data returned in the default C type. For CHAR data types, this is the same as the value in the LENGTH_PRECISION column.
SCALE SMALLINT The scale of the column. NULL is returned for data types where scale is not applicable.
PSEUDO_COLUMN SMALLINT Indicates whether the column is a pseudo-column; DB2 UDB CLI only returns:
  • SQL_PC_NOT_PSEUDO

Return codes

Diagnostics

Table 3. SQLSpecialColumns SQLSTATEs
SQLSTATE Description Explanation
24000 Cursor state that is not valid Cursor related information is requested, but no cursor is open.
40003 * Statement completion unknown The communication link between the CLI and the data source fails before the function completes processing.
HY001 Memory allocation failure The driver is unable to allocate memory required to support the processing or completion of the function.
HY009 Argument length that is not valid The value of one of the length arguments is less than 0, but not equal to SQL_NTS.
HY021 Internal descriptor that is not valid The internal descriptor cannot be addressed or allocated, or it contains a value that is not valid.
HYC00 Driver not capable The data source does not support the catalog portion (first part) of a three-part table name.