SQLStatistics - Get index and statistics information for a base table

 

SQLStatistics() retrieves index information for a given table. It also returns the cardinality and the number of pages associated with the table and the indexes on the table. The information is returned in a 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 SQLStatistics  (SQLHSTMT       hstmt,
                          SQLCHAR        *szCatalogName,
                          SQLSMALLINT    cbCatalogName,
                          SQLCHAR        *szSchemaName,
                          SQLSMALLINT    cbSchemaName,
                          SQLCHAR        *szTableName,
                          SQLSMALLINT    cbTableName,
                          SQLSMALLINT    fUnique,
                          SQLSMALLINT    fAccuracy);

 

Function arguments

Table 1. SQLStatistics arguments
Data type Argument Use Description
SQLHSTMT hstmt Input Statement handle.
SQLCHAR * szCatalogName Input Catalog qualifier of a three-part table name. This must be a null pointer or a zero length string.
SQLSMALLINT cbCatalogName Input Length of cbCatalogName. This must be set to 0.
SQLCHAR * szSchemaName Input Schema qualifier of the specified table.
SQLSMALLINT cbSchemaName Input Length of szSchemaName.
SQLCHAR * szTableName Input Table name.
SQLSMALLINT cbTableName Input Length of cbTableName.
SQLSMALLINT fUnique Input Type of index information to return:

  • SQL_INDEX_UNIQUE

    Only unique indexes are returned.

  • SQL_INDEX_ALL

    All indexes are returned.

SQLSMALLINT fAccuracy Input Not currently used, must be set to 0.

 

Usage

SQLStatistics() returns the following types of information:

Table 2. Columns returned by SQLStatistics
Column number/name Data type Description
1 TABLE_CAT VARCHAR(128) The name of the catalog containing TABLE_SCHEM. This is set to NULL.
2 TABLE_SCHEM VARCHAR(128) The name of the schema containing TABLE_NAME.
3 TABLE_NAME VARCHAR(128) not NULL Name of the table.
4 NON_UNIQUE SMALLINT This indicates whether the index prohibits duplicate values:

  • TRUE if the index allows duplicate values.

  • FALSE if the index values must be unique.

  • NULL is returned if the TYPE column indicates that this row is SQL_TABLE_STAT (statistics information about the table itself).
5 INDEX_QUALIFIER VARCHAR(128) The identifier used to qualify the index name. This is NULL if the TYPE column indicates SQL_TABLE_STAT.
6 INDEX_NAME VARCHAR(128) The name of the index. If the TYPE column has the value SQL_TABLE_STAT, this column has the value NULL.
7 TYPE SMALLINT not NULL This indicates the type of information contained in this row of the result set:

  • SQL_TABLE_STAT

    This indicates this row contains statistics information on the table itself.

  • SQL_INDEX_CLUSTERED

    This indicates this row contains information about an index, and the index type is a clustered index.

  • SQL_INDEX_HASHED

    This indicates this row contains information about an index, and the index type is a hashed index.

  • SQL_INDEX_OTHER

    This indicates this row contains information about an index, and the index type is other than clustered or hashed.

Currently, SQL_INDEX_OTHER is the only possible type.

8 ORDINAL_POSITION SMALLINT Ordinal position of the column within the index whose name is given in the INDEX_NAME column. A NULL value is returned for this column if the TYPE column has the value of SQL_TABLE_STAT.
9 COLUMN_NAME VARCHAR(128) Name of the column in the index.
10 COLLATION CHAR(1) Sort sequence for the column; "A" for ascending, "D" for descending. NULL value is returned if the value in the TYPE column is SQL_TABLE_STAT.
11 CARDINALITY INTEGER

  • If the TYPE column contains the value SQL_TABLE_STAT, this column contains the number of rows in the table.

  • If the TYPE column value is not SQL_TABLE_STAT, this column contains the number of unique values in the index.

  • A NULL value is returned if information is not available from the Database Management System (DBMS).
12 PAGES INTEGER

  • If the TYPE column contains the value SQL_TABLE_STAT, this column contains the number of pages used to store the table.

  • If the TYPE column value is not SQL_TABLE_STAT, this column contains the number of pages used to store the indexes.

  • A NULL value is returned if information is not available from the DBMS.

For the row in the result set that contains table statistics (TYPE is set to SQL_TABLE_STAT), the columns values of NON_UNIQUE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION, COLUMN_NAME, and COLLATION are set to NULL. If the CARDINALITY or PAGES information cannot be determined, then NULL is returned for those columns.

 

Return codes

 

Diagnostics

Table 3. SQLStatistics 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 or buffer length that is not valid The value of one of the name 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 catalog part (the first part) of a three-part table name is not supported by the data source.

 

Parent topic:

DB2 UDB CLI functions