DB2 10.5 for Linux, UNIX, and Windows

SQLColAttribute function (CLI) - Return a column attribute

Returns descriptor information for a column in a result set. Descriptor information is returned as a character string, a 32-bit descriptor-dependent value, or an integer value.

Specification:

Unicode equivalent: You can also use this function with the Unicode character set. The corresponding Unicode function is SQLColAttributeW(). See Unicode functions (CLI) for information about ANSI to Unicode function mappings.

Syntax

In a Windows 64-bit environment, the syntax is as follows:
SQLRETURN   SQLColAttribute  (
                SQLHSTMT          StatementHandle,       /* hstmt */
                SQLSMALLINT       ColumnNumber,          /* icol */
                SQLSMALLINT       FieldIdentifier,       /* fDescType */
                SQLPOINTER        CharacterAttributePtr, /* rgbDesc */
                SQLSMALLINT       BufferLength,          /* cbDescMax */
                SQLSMALLINT       *StringLengthPtr,      /* pcbDesc */
                SQLLEN            *NumericAttributePtr); /* pfDesc */
The syntax for all other platforms is as follows:
SQLRETURN   SQLColAttribute  (
                SQLHSTMT          StatementHandle,       /* hstmt */
                SQLSMALLINT       ColumnNumber,          /* icol */
                SQLSMALLINT       FieldIdentifier,       /* fDescType */
                SQLPOINTER        CharacterAttributePtr, /* rgbDesc */
                SQLSMALLINT       BufferLength,          /* cbDescMax */
                SQLSMALLINT       *StringLengthPtr,      /* pcbDesc */
                SQLPOINTER        NumericAttributePtr);  /* pfDesc */

Function arguments

Table 1. SQLColAttribute arguments
Data type Argument Use Description
SQLHSTMT StatementHandle input Statement handle.
SQLUSMALLINT ColumnNumber input

The number of the record in the IRD from which the field value is to be retrieved. This argument corresponds to the column number of result data, ordered sequentially from left to right, starting at 1. Columns can be described in any order.

Column 0 can be specified in this argument, but all values except SQL_DESC_TYPE and SQL_DESC_OCTET_LENGTH return undefined values.

SQLSMALLINT FieldIdentifier input The field in row ColumnNumber of the IRD that is to be returned (see Table 2).
SQLPOINTER CharacterAttributePtr output Pointer to a buffer in which to return the value in the FieldIdentifier field of the ColumnNumber row of the IRD, if the field is a character string. Otherwise, the field is unused.
SQLINTEGER BufferLength input Number of SQLCHAR elements (or SQLWCHAR elements for the Unicode variant of this function) needed to store the *CharacterAttributePtr buffer, if the field is a character string. Otherwise, the field is ignored.
SQLSMALLINT * StringLengthPtr output

Pointer to a buffer in which to return the total number of bytes (excluding the byte count of the null termination character for character data) available to return in *CharacterAttributePtr.

For character data, if the number of bytes available to return is greater than or equal to BufferLength, the descriptor information in *CharacterAttributePtr is truncated to BufferLength minus the length of a null termination character and is null-terminated by CLI.

For all other types of data, the value of BufferLength is ignored and CLI assumes the size of *CharacterAttributePtr is 32 bits.

SQLLEN* (Window 64-bit) or SQLPOINTER NumericAttributePtr output Pointer to a buffer in which to return the value in the FieldIdentifier field of the ColumnNumber row of the IRD, if the field is a numeric descriptor type, such as SQL_DESC_COLUMN_LENGTH. Otherwise, the field is unused.

Usage

SQLColAttribute() returns information either in *NumericAttributePtr or in *CharacterAttributePtr. Integer information is returned in *NumericAttributePtr as a 32-bit, signed value; all other formats of information are returned in *CharacterAttributePtr. When information is returned in *NumericAttributePtr, CLI ignores CharacterAttributePtr, BufferLength, and StringLengthPtr. When information is returned in *CharacterAttributePtr, CLI ignores NumericAttributePtr.

SQLColAttribute() returns values from the descriptor fields of the IRD. The function is called with a statement handle rather than a descriptor handle. The values returned by SQLColAttribute() for the FieldIdentifier values, listed in the following table, can also be retrieved by calling SQLGetDescField() with the appropriate IRD handle.

The currently defined descriptor types, the version of CLI in which they were introduced (perhaps with a different name), and the arguments in which information is returned for them are shown in the following table; it is expected that more descriptor types will be defined to take advantage of different data sources.

CLI must return a value for each of the descriptor types. If a descriptor type does not apply to a data source, unless otherwise stated, CLI returns 0 in *StringLengthPtr or an empty string in *CharacterAttributePtr.

The following table lists the descriptor types returned by SQLColAttribute().
Table 2. SQLColAttribute arguments
FieldIdentifier Information returned in Description
SQL_DESC_AUTO_UNIQUE_VALUE Numeric AttributePtr Indicates if the column data type is an auto increment data type.

SQL_TRUE is returned in NumericAttributePtr for identity columns with the GENERATED ALWAYS identity. SQL_FALSE is returned in all other cases.

SQL_DESC_BASE_COLUMN_NAME Character AttributePtr

The base column name for the set column. If a base column name does not exist (as in the case of columns that are expressions), then this variable contains an empty string.

This information is returned from the SQL_DESC_BASE_COLUMN_NAME record field of the IRD, which is a read-only field.

SQL_DESC_BASE_TABLE_NAME Character AttributePtr The name of the base table that contains the column. If the base table name cannot be defined or is not applicable, then this variable contains an empty string.
SQL_DESC_CASE_SENSITIVE Numeric AttributePtr Indicates if the column data type is a case-sensitive data type.

Either SQL_TRUE or SQL_FALSE can be returned in the NumericAttributePtr buffer depending on the data type.

Case sensitivity does not apply to graphic data types.

SQL_FALSE is returned for non-character data types and for the XML data type.

SQL_DESC_CATALOG_NAME Character AttributePtr An empty string is returned since the CLI driver only supports two-part names for a table.
SQL_DESC_CONCISE_TYPE Numeric AttributePtr

The concise data type.

For the datetime data types, this field returns the concise data type, for example, SQL_TYPE_TIME.

This information is returned from the SQL_DESC_CONCISE_TYPE record field of the IRD.

SQL_DESC_COUNT Numeric AttributePtr The number of columns in the result set is returned in NumericAttributePtr.
SQL_DESC_DISPLAY_SIZE Numeric AttributePtr The maximum number of bytes needed to display the data in character form is returned in NumericAttributePtr.

Refer to the data type display size table for the display size of each of the column types.

SQL_DESC_DISTINCT_TYPE Character AttributePtr The user defined distinct type name of the column is returned in CharacterAttributePtr. If the column is a built-in SQL type and not a user defined distinct type, an empty string is returned.
Note: This is an IBM defined extension to the list of descriptor attributes defined by ODBC.
SQL_DESC_FIXED_PREC_SCALE Numeric AttributePtr

SQL_TRUE if the column has a fixed precision and non-zero scale that are data-source-specific.

SQL_FALSE if the column does not have a fixed precision and non-zero scale that are data-source-specific.

SQL_FALSE is returned in NumericAttributePtr for all DB2® SQL data types.

SQL_DESC_IDENTITY_VALUE Numeric AttributePtr

Indicates whether the column is an identity column.

SQL_TRUE is returned in the NumericAttributePtr buffer when a column is an identity column.

SQL_FALSE is returned in the NumericAttributePtr buffer when a column is not an identity column.

SQL_DESC_LABEL Character AttributePtr The column label is returned in CharacterAttributePtr. If the column does not have a label, the column name or the column expression is returned. If the column is unlabeled and unnamed, an empty string is returned.
SQL_DESC_LENGTH Numeric AttributePtr

A numeric value that is either the maximum or actual element (SQLCHAR or SQLWCHAR) length of a character string or binary data type. It is the maximum element length for a fixed-length data type, or the actual element length for a variable-length data type. Its value always excludes the null termination byte that ends the character string.

If a column is defined with the CODEUNITS32 unit, a numeric value specifies the number of code units that the column can store.

This information is returned from the SQL_DESC_LENGTH record field of the IRD.

This value is 0 for the XML data type.

SQL_DESC_LITERAL_PREFIX Character AttributePtr

This VARCHAR(128) record field contains the character or characters that CLI recognizes as a prefix for a literal of this data type. This field contains an empty string for a data type for which a literal prefix is not applicable.

SQL_DESC_LITERAL_SUFFIX Character AttributePtr This VARCHAR(128) record field contains the character or characters that CLI recognizes as a suffix for a literal of this data type. This field contains an empty string for a data type for which a literal suffix is not applicable.
SQL_DESC_LOCAL_TYPE_NAME Character AttributePtr This VARCHAR(128) record field contains any localized (native language) name for the data type that might be different from the regular name of the data type. If there is no localized name, then an empty string is returned. This field is for display purposes only. The character set of the string is locale-dependent and is typically the default character set of the server.
SQL_DESC_NAME Character AttributePtr The name of the column ColumnNumber is returned in CharacterAttributePtr. If the column is an expression, then the column number is returned.

In either case, SQL_DESC_UNNAMED is set to SQL_NAMED. If there is no column name or a column alias, an empty string is returned and SQL_DESC_UNNAMED is set to SQL_UNNAMED.

This information is returned from the SQL_DESC_NAME record field of the IRD.

SQL_DESC_NULLABLE Numeric AttributePtr If the column identified by ColumnNumber can contain nulls, then SQL_NULLABLE is returned in NumericAttributePtr.

If the column is constrained not to accept nulls, then SQL_NO_NULLS is returned in NumericAttributePtr.

This information is returned from the SQL_DESC_NULLABLE record field of the IRD.

SQL_DESC_NUM_PREC_RADIX Numeric AttributePtr

If the data type in the SQL_DESC_TYPE field is an approximate data type, this SQLINTEGER field contains a value of 2 because the SQL_DESC_PRECISION field contains the number of bits.

If the data type in the SQL_DESC_TYPE field is an exact numeric data type, this field contains a value of 10 because the SQL_DESC_PRECISION field contains the number of decimal digits.

SQL_DESC_OCTET_LENGTH Numeric AttributePtr The number of bytes of data that is associated with the column is returned in the NumericAttributePtr argument. The SQL_DESC_OCTET_LENGTH field is the length in bytes of data that is transferred on the fetch or the SQLGetData() call for the column if SQL_C_DEFAULT is specified as the C data type. Refer to data type length table for the length of each of the SQL data types.

If the column identified in the ColumnNumber argument is a fixed-length character or binary string, (for example, SQL_CHAR or SQL_BINARY) the actual length is returned.

If the column identified in the ColumnNumber argument is a variable length character or binary string, (for example, SQL_VARCHAR or SQL_BLOB) the maximum length is returned.

If the column identified in the ColumnNumber argument is of type SQL_XML, 0 is returned.

If the column identified in the ColumnNumber argument is defined with the CODEUNITS32 unit, a numeric value specifies the maximum number of bytes that the column can store.

SQL_DESC_PRECISION Numeric AttributePtr The precision in units of digits is returned in NumericAttributePtr if the column is SQL_DECIMAL, SQL_NUMERIC, SQL_DOUBLE, SQL_FLOAT, SQL_INTEGER, SQL_REAL or SQL_SMALLINT.

If the column is a character SQL data type, then the precision returned in NumericAttributePtr, indicates the maximum number of SQLCHAR or SQLWCHAR elements the column can hold.

If the column is a graphic SQL data type, then the precision returned in NumericAttributePtr, indicates the maximum number of double-byte elements the column can hold.

If the column is the XML data type, the precision is 0.

Refer to data type precision table for the precision of each of the SQL data types.

This information is returned from the SQL_DESC_PRECISION record field of the IRD.

SQL_DESC_SCALE Numeric AttributePtr The scale attribute of the column is returned. Refer to the data type scale table for the scale of each of the SQL data types.

This information is returned from the SCALE record field of the IRD.

SQL_DESC_SCHEMA_NAME Character AttributePtr The schema of the table that contains the column is returned in CharacterAttributePtr. The name of the schema that contains the table is returned. If the schema name is of less than 8 characters, then spaces are appended as extra characters.
SQL_DESC_SEARCHABLE Numeric AttributePtr Indicates if the column data type is searchable.

SQL_PRED_NONE (SQL_UNSEARCHABLE in DB2 CLI v2) if the column cannot be used in a WHERE clause.

SQL_PRED_CHAR (SQL_LIKE_ONLY in DB2 CLI v2) if the column can be used in a WHERE clause only with the LIKE predicate.

SQL_PRED_BASIC (SQL_ALL_EXCEPT_LIKE in DB2 CLI v2) if the column can be used in a WHERE clause with all comparison operators except LIKE.

SQL_SEARCHABLE if the column can be used in a WHERE clause with any comparison operator.

SQL_DESC_TABLE_NAME Character AttributePtr The name of the table that contains the column is returned. If the table name cannot be defined or is not applicable, then this variable contains an empty string.
SQL_DESC_TYPE Numeric AttributePtr The SQL data type of the column identified in ColumnNumber is returned in NumericAttributePtr. The possible values returned are listed in table of symbolic and default data types for CLI.

When ColumnNumber is equal to 0, SQL_BINARY is returned for variable-length bookmarks, and SQL_INTEGER is returned for fixed-length bookmarks.

For the datetime data types, this field returns the verbose data type, for example, SQL_DATETIME.

This information is returned from the SQL_DESC_TYPE record field of the IRD.

SQL_DESC_TYPE_NAME Character AttributePtr The type of the column (as entered in an SQL statement) is returned in CharacterAttributePtr.

For information about each data type, refer to the list of symbolic and default data types for CLI.

SQL_DESC_UNNAMED Numeric AttributePtr

SQL_NAMED or SQL_UNNAMED. If the SQL_DESC_NAME field of the IRD contains a column alias, or a column name, SQL_NAMED is returned. If there is no column name or a column alias, SQL_UNNAMED is returned.

This information is returned from the SQL_DESC_UNNAMED record field of the IRD.

SQL_DESC_UNSIGNED Numeric AttributePtr Indicates if the column data type is an unsigned type or not.

SQL_TRUE is returned in NumericAttributePtr for all non-numeric data types, SQL_FALSE is returned for all numeric data types.

SQL_DESC_UPDATABLE Numeric AttributePtr
Indicates if the column data type is an updatable data type:
  • SQL_ATTR_READONLY is returned if the result set column is read-only.
  • SQL_ATTR_WRITE is returned if the result set column is read-write.
  • SQL_ATTR_READWRITE_UNKNOWN is returned if it is not known whether the result set column is updatable or not.

This function is an extensible alternative to SQLDescribeCol(). SQLDescribeCol() returns a fixed set of descriptor information based on ANSI-89 SQL. SQLColAttribute() allows access to the more extensive set of descriptor information available in ANSI SQL-92 and DBMS vendor extensions.

Return codes

  • SQL_SUCCESS
  • SQL_SUCCESS_WITH_INFO
  • SQL_STILL_EXECUTING
  • SQL_ERROR
  • SQL_INVALID_HANDLE

Diagnostics

Table 3. SQLColAttribute SQLSTATEs
SQLSTATE Description Explanation
01000 Warning. Informational message. (Function returns SQL_SUCCESS_WITH_INFO.)
01004 Data truncated. The buffer *CharacterAttributePtr was not large enough to return the entire string value, so the string was truncated. The length of the untruncated string value is returned in *StringLengthPtr. (Function returns SQL_SUCCESS_WITH_INFO.)
07005 The statement did not return a result set. The statement associated with the StatementHandle did not return a result set. There were no columns to describe.
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.
HY000 General error. An error occurred for which there was no specific SQLSTATE. The error message returned by SQLGetDiagRec() in the *MessageText buffer describes the error and its cause.
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 about process-level memory limitations.
HY008 Operation was Canceled. Asynchronous processing was enabled for StatementHandle. The SQLCancel() function was called on the StatementHandle from a different thread before the function completed execution.
HY010 Function sequence error. The function was called before calling SQLPrepare() or SQLExecDirect() for the StatementHandle.

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

SQLExecute() or SQLExecDirect() was called for the StatementHandle and returned SQL_NEED_DATA. This function was called before data was sent for all data-at-execution parameters or columns.

HY090 Invalid string or buffer length. The value specified for the argument BufferLength was less than 0.
HY091 Invalid descriptor field identifier. The value specified for the argument FieldIdentifier was not one of the defined values, and was not an implementation-defined value.
HYC00 Driver not capable. The value specified for the argument FieldIdentifier was not supported by CLI.

The SQLColAttribute() function that is called between the SQLPrepare() or SQLExecute() function can return any SQLSTATE that is associated with either the SQLPrepare() or SQLExecute() function.

The call to the SQLColAttribute() function before you execute a statement can have a negative performance impact.

Restrictions

None.

Example

    /* get display size for column */
    cliRC = SQLColAttribute(hstmt,
                            (SQLSMALLINT)(i + 1),
                            SQL_DESC_DISPLAY_SIZE,
                            NULL,
                            0,
                            NULL,
                            &colDataDisplaySize)