SQLGetTypeInfo()
- Get data type information
SQLGetTypeInfo()
returns information about
the data types that are supported by the database management systems
that are associated with Db2 ODBC.
This information is returned in an SQL result set. The columns of
this result set can be received by using the same functions that you
use to process a query.
ODBC specifications for SQLGetTypeInfo()
ODBC specification level | In X/Open CLI CAE specification? | In ISO CLI specification? |
---|---|---|
1.0 | Yes | Yes |
Syntax
SQLRETURN SQLGetTypeInfo (SQLHSTMT hstmt,
SQLSMALLINT fSqlType);
Function arguments
The following table lists the data type, use, and description for each argument in this function.
Data type | Argument | Use | Description |
---|---|---|---|
SQLHSTMT | hstmt | input | Specifies a statement handle. |
SQLSMALLINT | fSqlType | input | Specifies the SQL data type that is queried. The
following values that specify data types are supported:
|
Usage
Because SQLGetTypeInfo()
generates
a result set it is essentially equivalent to executing a query. Like
a query, calling SQLGetTypeInfo()
generates a cursor
and begins a transaction. To prepare and execute another statement
on this statement handle, the cursor must be closed.
If you
call SQLGetTypeInfo()
with an invalid value in the fSqlType argument,
an empty result set is returned.
Table 3 describes each column in the result set that this function generates.
Although new columns might be added and the names of the existing columns might be changed in future releases, the position of the current columns does not change. The data types that are returned are those that can be used in a CREATE TABLE or ALTER TABLE, statement. Nonpersistent data types such as the locator data types are not part of the returned result set. User-defined data types are not returned either.
Position | Column name | Data type | Description |
---|---|---|---|
1 | TYPE_NAME | VARCHAR(128) NOT NULL | Contains a character representation of the SQL Data Definition Language data type name. For example, VARCHAR, BLOB, DATE, INTEGER. |
2 | DATA_TYPE | SMALLINT NOT NULL | Contains the SQL data type definition values. For example, SQL_VARCHAR, SQL_BLOB, SQL_TYPE_DATE, SQL_INTEGER. |
3 | COLUMN_SIZE | INTEGER | If the data type
is a character or binary string, this column contains the maximum
length in bytes. If this data type is a graphic (DBCS) string, this
column contains the number of double-byte characters for the column.
If the data type is XML, zero is returned. For date, time, timestamp data types, this is the total number of characters required to display the value when converted to characters. For numeric data types, this column contains the total number of digits. |
4 | LITERAL_PREFIX | VARCHAR(128) | Contains the character that Db2 recognizes as a prefix for a literal of this data type. This column is null for data types where a literal prefix is not applicable. |
5 | LITERAL_SUFFIX | VARCHAR(128) | Contains the character that Db2 recognizes as a suffix for a literal of this data type. This column is null for data types where a literal suffix is not applicable. |
6 | CREATE_PARAMS | VARCHAR(128) | Contains a list of values, that are separated by
commas. These values correspond to each parameter that you can specify
for a data type in a CREATE TABLE or an ALTER TABLE SQL statement.
One or more of the following values appear in this result-set column:
Usage: The CREATE_PARAMS column enables you to customize the interface of Data Definition Language builders in your applications. A Data Definition Language builder is a piece of your application that creates database objects, such as tables. Use the CREATE_PARAMS to determine the number of arguments that are required to define a data type, then use localized text to label the controls on the Data Definition Language builder. |
7 | NULLABLE | SMALLINT NOT NULL | Indicates whether the data type accepts a null
value. This column contains one of the following values:
|
8 | CASE_SENSITIVE | SMALLINT NOT NULL | Indicates whether the data type can be treated
as case sensitive for collation purposes. This column contains one
of the following values:
|
9 | SEARCHABLE | SMALLINT NOT NULL | Indicates how the data type is used in a WHERE
clause. This column contains one of the following values:
|
10 | UNSIGNED_ATTRIBUTE | SMALLINT | Indicates whether the data type is unsigned. This
column contains one of the following values:
|
11 | FIXED_PREC_SCALE | SMALLINT NOT NULL | Contains the value SQL_TRUE if the data type is exact numeric and always has the same precision and scale; otherwise, it contains SQL_FALSE. |
12 | AUTO_INCREMENT | SMALLINT | Contains SQL_TRUE if a column of this data type is automatically set to a unique value when a row is inserted; otherwise, contains SQL_FALSE. |
13 | LOCAL_TYPE_NAME | VARCHAR(128) | Contains any localized (native language) name for
the data type that is different from the regular name of the data
type. If there is no localized name, this column contains a null indicator.
This column is intended for display only. The character set of the string is locale-dependent and is typically the default character set of the database. |
14 | MINIMUM_SCALE | SMALLINT | Contains the minimum scale of the SQL data type. If a data type has a fixed scale, the MINIMUM_SCALE and MAXIMUM_SCALE columns both contain the same value. NULL is returned where scale is not applicable. |
15 | MAXIMUM_SCALE | SMALLINT | Contains the maximum scale of the SQL data type. NULL is returned where scale is not applicable. If the maximum scale is not defined separately in the database management system, but is defined instead to be the same as the maximum length of the column, then this column contains the same value as the COLUMN_SIZE column. |
Return codes
SQLGetTypeInfo()
,
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.
SQLSTATE | Description | Explanation |
---|---|---|
08S01 | Communication link failure. | The communication link between the application and data source fails before the function completes. |
24000 | Invalid cursor state. | A cursor is open on the statement handle. |
HY001 | Memory allocation failure. | Db2 ODBC is not able to allocate the required memory to support the execution or the completion of the function. |
HY004 | Invalid SQL data type. | An invalid value for the fSqlType argument is specified. |
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.) |
Restrictions
- Data type
- fSqlType
- TINYINT
- SQL_TINYINT
- BIT
- SQL_BIT
Example
SQLGetTypeInfo()
to check
which ODBC data types the database management system supports. /******************************************************************/
/* Invoke SQLGetTypeInfo to retrieve SQL data types supported. */
/******************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlca.h>
#include "sqlcli1.h"
/******************************************************************/
/* Invoke SQLGetTypeInfo to retrieve all SQL data types supported */
/* by data source. */
/******************************************************************/
int main( )
{
SQLHENV hEnv = SQL_NULL_HENV;
SQLHDBC hDbc = SQL_NULL_HDBC;
SQLHSTMT hStmt = SQL_NULL_HSTMT;
SQLRETURN rc = SQL_SUCCESS;
SQLINTEGER RETCODE = 0;
(void) printf ("**** Entering CLIP06.\n\n");
/*****************************************************************/
/* Allocate environment handle */
/*****************************************************************/
RETCODE = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
if (RETCODE != SQL_SUCCESS)
goto dberror;
/*****************************************************************/
/* Allocate connection handle to DSN */
/*****************************************************************/
RETCODE = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc);
if( RETCODE != SQL_SUCCESS ) // Could not get a Connect Handle
goto dberror;
/*****************************************************************/
/* CONNECT TO data source (STLEC1) */
/*****************************************************************/
RETCODE = SQLConnect(hDbc, // Connect handle
(SQLCHAR *) "STLEC1", // DSN
SQL_NTS, // DSN is nul-terminated
NULL, // Null UID
0 ,
NULL, // Null Auth string
0);
if( RETCODE != SQL_SUCCESS ) // Connect failed
goto dberror;
/*****************************************************************/
/* Retrieve SQL data types from DSN */
/*****************************************************************/
// local variables to Bind to retrieve TYPE_NAME, DATA_TYPE,
// COLUMN_SIZE and NULLABLE
struct // TYPE_NAME is VARCHAR(128)
{
SQLSMALLINT length;
SQLCHAR name [128];
SQLINTEGER ind;
} typename;
SQLSMALLINT data_type; // DATA_TYPE is SMALLINT
SQLINTEGER data_type_ind;
SQLINTEGER column_size; // COLUMN_SIZE is integer
SQLINTEGER column_size_ind;
SQLSMALLINT nullable; // NULLABLE is SMALLINT
SQLINTEGER nullable_ind;
/*****************************************************************/
/* Allocate statement handle */
/*****************************************************************/
rc = SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt);
if (rc != SQL_SUCCESS)
goto exit;
/*****************************************************************/
/* */
/* Retrieve native SQL types from DSN ------------> */
/* */
/* The result set consists of 15 columns. We only bind */
/* TYPE_NAME, DATA_TYPE, COLUMN_SIZE and NULLABLE. Note: Need */
/* not bind all columns of result set -- only those required. */
/* */
/*****************************************************************/
rc = SQLGetTypeInfo (hStmt,
SQL_ALL_TYPES);
if (rc != SQL_SUCCESS)
goto exit;
rc = SQLBindCol (hStmt, // bind TYPE_NAME
1,
SQL_CHAR,
(SQLPOINTER) typename.name,
128,
&typename.ind);
if (rc != SQL_SUCCESS)
goto exit;
rc = SQLBindCol (hStmt, // bind DATA_NAME
2,
SQL_C_DEFAULT,
(SQLPOINTER) &data_type,
sizeof(data_type),
&data_type_ind);
if (rc != SQL_SUCCESS)
goto exit;rc = SQLBindCol (hStmt, // bind COLUMN_SIZE
3,
SQL_C_DEFAULT,
(SQLPOINTER) &column_size,
sizeof(column_size),
&column_size_ind);
if (rc != SQL_SUCCESS)
goto exit;
rc = SQLBindCol (hStmt, // bind NULLABLE
7,
SQL_C_DEFAULT,
(SQLPOINTER) &nullable,
sizeof(nullable),
&nullable_ind);
if (rc != SQL_SUCCESS)
goto exit;
/*****************************************************************/
/* Fetch all native DSN SQL Types and print Type Name, Type, */
/* Precision and nullability. */
/*****************************************************************/
while ((rc = SQLFetch (hStmt)) == SQL_SUCCESS)
{
(void) printf ("**** Type Name is %s. Type is %d. Precision is %d.",
typename.name,
data_type,
column_size);
if (nullable == SQL_NULLABLE)
(void) printf (" Type is nullable.\n");
else
(void) printf (" Type is not nullable.\n");
}
if (rc == SQL_NO_DATA_FOUND) // if result set exhausted reset
rc = SQL_SUCCESS; // rc to OK
/*****************************************************************/
/* Free statement handle */
/*****************************************************************/
rc = SQLFreeHandle(SQL_HANDLE_STMT, hStmt);
if (RETCODE != SQL_SUCCESS) // An advertised API failed
goto dberror;
/*****************************************************************/
/* DISCONNECT from data source */
/*****************************************************************/
RETCODE = SQLDisconnect(hDbc);
if (RETCODE != SQL_SUCCESS)
goto dberror;
/*****************************************************************/
/* Deallocate connection handle */
/*****************************************************************/
RETCODE = SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
if (RETCODE != SQL_SUCCESS)
goto dberror;
/*****************************************************************/
/* Free environment handle */
/*****************************************************************/
RETCODE = SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
if (RETCODE == SQL_SUCCESS)
goto exit;
dberror:
RETCODE=12;
exit:
(void) printf ("**** Exiting CLIP06.\n\n");
return(RETCODE);
}