Edit

Share via


ODBC API reference

Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance

The ODBC API provides a standard set of functions for connecting to data sources, executing SQL statements, and retrieving results. Each function is a C programming language function with descriptions that include purpose, ODBC version, syntax, arguments, return values, diagnostics, and code examples.

The standard CLI conformance level can be one of the following: ISO 92, Open Group, ODBC, or Deprecated. A function tagged as ISO 92-conformant also appears in Open Group version 1, because Open Group is a pure superset of ISO 92. A function tagged as Open Group-compliant also appears in ODBC 3.x, because ODBC 3.x is a pure superset of Open Group version 1. A function tagged as ODBC-compliant doesn't appear in either standard. A function tagged as deprecated was deprecated in ODBC 3.x.

The SQLGetDiagField function description describes how to handle diagnostic information. The text associated with SQLSTATE values provides a description of the condition but doesn't prescribe specific text.

Note

For driver-specific information about ODBC functions, see the section for the driver.

Handle allocation and management

These functions allocate and free handles for environments, connections, statements, and descriptors. Handles are the primary mechanism for tracking state in ODBC applications.

Function Description
SQLAllocHandle Allocates an environment, connection, statement, or descriptor handle. This is the ODBC 3.x function that replaces the deprecated allocation functions.
SQLFreeHandle Frees an environment, connection, statement, or descriptor handle and releases associated resources.
SQLAllocConnect Allocates a connection handle. Deprecated in ODBC 3.x; use SQLAllocHandle instead.
SQLAllocEnv Allocates an environment handle. Deprecated in ODBC 3.x; use SQLAllocHandle instead.
SQLAllocStmt Allocates a statement handle. Deprecated in ODBC 3.x; use SQLAllocHandle instead.
SQLFreeConnect Frees a connection handle. Deprecated in ODBC 3.x; use SQLFreeHandle instead.
SQLFreeEnv Frees an environment handle. Deprecated in ODBC 3.x; use SQLFreeHandle instead.
SQLFreeStmt Stops statement processing, closes associated cursors, discards pending results, and optionally frees resources associated with a statement handle.

Connection functions

These functions establish and manage connections to data sources. They support various connection methods including standard connections, driver-specific dialogs, and iterative browsing.

Function Description
SQLConnect Establishes a connection to a data source using a data source name, user ID, and password.
SQLDriverConnect Establishes a connection using a connection string. Supports driver-specific dialogs for additional connection information.
SQLBrowseConnect Supports an iterative method of discovering and enumerating the attributes needed to connect to a data source.
SQLDisconnect Closes a connection to a data source and releases associated resources.
SQLDataSources Returns a list of available data sources. Called on the Driver Manager, not a specific driver.
SQLDrivers Returns a list of installed drivers and their attributes. Called on the Driver Manager.

Statement preparation and execution

These functions prepare and execute SQL statements. ODBC supports both direct execution and prepared execution, with prepared execution offering better performance for repeatedly executed statements.

Function Description
SQLPrepare Prepares a SQL statement for later execution. The data source compiles and optimizes the statement.
SQLExecute Executes a prepared statement. Call SQLPrepare before calling this function.
SQLExecDirect Prepares and executes a SQL statement in a single call. Use for statements executed only once.
SQLNativeSql Returns the SQL string as modified by the driver, showing how the driver translates ODBC SQL syntax.
SQLCancel Cancels processing on a statement. Can cancel an asynchronously executing function or a function running on another thread.
SQLCancelHandle Cancels processing on a connection or statement. More flexible than SQLCancel for canceling connection functions.
SQLCompleteAsync Determines when an asynchronous function completes. Used with notification-based asynchronous processing.

Parameter binding

These functions bind application variables to parameter markers in SQL statements. Parameters enable dynamic values in prepared statements.

Function Description
SQLBindParameter Binds an application variable to a parameter marker in a SQL statement. Supports input, output, and input/output parameters.
SQLNumParams Returns the number of parameters in a SQL statement.
SQLDescribeParam Returns the description of a parameter marker, including data type, size, and precision.
SQLParamData Used with SQLPutData to supply parameter data at execution time. Returns the parameter needing data.
SQLPutData Sends part or all of a data value for a parameter at execution time. Supports large data in chunks.
SQLSetParam Binds a parameter. Deprecated in ODBC 3.x; use SQLBindParameter instead.
SQLParamOptions Sets options for parameter arrays. Deprecated in ODBC 3.x; use statement attributes instead.

Result set binding and retrieval

These functions bind application buffers to result set columns and retrieve data from query results.

Function Description
SQLBindCol Binds an application variable to a result set column for subsequent fetch operations.
SQLFetch Fetches the next rowset of data from the result set into bound columns.
SQLFetchScroll Fetches the specified rowset from a result set. Supports scrolling to first, last, next, prior, absolute, and relative positions.
SQLGetData Retrieves data for a single column after SQLFetch or SQLFetchScroll. Useful for large data or unbound columns.
SQLExtendedFetch Fetches the specified rowset of data. Deprecated in ODBC 3.x; use SQLFetchScroll instead.
SQLMoreResults Determines whether more results are available on a statement and advances to the next result set.
SQLRowCount Returns the number of rows affected by an UPDATE, INSERT, or DELETE statement.

Cursor operations

These functions manage cursor behavior, positioning, and bulk operations on rowsets.

Function Description
SQLSetPos Sets the cursor position within a rowset and allows applications to refresh, update, or delete data at that position.
SQLBulkOperations Performs bulk insert, update, delete, or fetch-by-bookmark operations on rowsets.
SQLCloseCursor Closes a cursor that has been opened on a statement and discards pending results.
SQLGetCursorName Returns the cursor name associated with a statement.
SQLSetCursorName Specifies a cursor name for positioned UPDATE and DELETE statements.
SQLSetScrollOptions Sets options for cursor behavior. Deprecated in ODBC 3.x; use statement attributes instead.

Catalog functions

These functions retrieve metadata about the database structure, including tables, columns, indexes, privileges, and stored procedures.

Function Description
SQLTables Returns a list of table names in the data source. Supports filtering by catalog, schema, and table type.
SQLColumns Returns a list of column names and their attributes for specified tables.
SQLPrimaryKeys Returns the columns that make up the primary key for a table.
SQLForeignKeys Returns foreign keys in a table or foreign keys in other tables that reference a table's primary key.
SQLStatistics Returns statistics about a table and a list of indexes associated with it.
SQLSpecialColumns Returns columns that uniquely identify a row or columns that are automatically updated when any value in the row is updated.
SQLColumnPrivileges Returns a list of columns and associated privileges for a table.
SQLTablePrivileges Returns a list of tables and the privileges associated with each table.
SQLProcedures Returns a list of stored procedure names in the data source.
SQLProcedureColumns Returns the list of input/output parameters and columns in the result set for specified procedures.
SQLGetTypeInfo Returns information about data types supported by the data source.

Descriptor operations

These functions get and set descriptor values. Descriptors contain metadata about parameters and result set columns.

Function Description
SQLGetDescField Returns the value of a single field of a descriptor record.
SQLGetDescRec Returns multiple fields of a descriptor record in a single call.
SQLSetDescField Sets the value of a single field of a descriptor record.
SQLSetDescRec Sets multiple fields of a descriptor record in a single call.
SQLCopyDesc Copies descriptor information from one descriptor handle to another.

Attribute functions

These functions get and set attributes for environments, connections, and statements. Attributes control various aspects of ODBC behavior.

Function Description
SQLSetEnvAttr Sets an environment attribute that affects all connections under that environment.
SQLGetEnvAttr Returns the value of an environment attribute.
SQLSetConnectAttr Sets a connection attribute that affects the connection and statements on it.
SQLGetConnectAttr Returns the value of a connection attribute.
SQLSetStmtAttr Sets a statement attribute. Includes cursor, query timeout, and parameter settings.
SQLGetStmtAttr Returns the value of a statement attribute.
SQLSetConnectOption Sets a connection option. Deprecated in ODBC 3.x; use SQLSetConnectAttr instead.
SQLGetConnectOption Returns the value of a connection option. Deprecated in ODBC 3.x; use SQLGetConnectAttr instead.
SQLSetStmtOption Sets a statement option. Deprecated in ODBC 3.x; use SQLSetStmtAttr instead.
SQLGetStmtOption Returns the value of a statement option. Deprecated in ODBC 3.x; use SQLGetStmtAttr instead.

Diagnostic and information functions

These functions retrieve diagnostic information, error messages, driver capabilities, and data source information.

Function Description
SQLGetDiagField Returns the value of a field in a diagnostic record containing error, warning, and status information.
SQLGetDiagRec Returns several commonly used fields of a diagnostic record, including SQLSTATE, native error code, and message text.
SQLError Returns error information. Deprecated in ODBC 3.x; use SQLGetDiagRec instead.
SQLGetFunctions Returns information about whether a driver supports a specific ODBC function.
SQLGetInfo Returns general information about the driver and data source, including supported features and capabilities.

Result set metadata

These functions return information about the structure of result sets.

Function Description
SQLNumResultCols Returns the number of columns in a result set.
SQLDescribeCol Returns the column name, data type, precision, scale, and nullability for a result set column.
SQLColAttribute Returns descriptor information for a column in a result set. More flexible than SQLDescribeCol.
SQLColAttributes Returns attributes for a column. Deprecated in ODBC 3.x; use SQLColAttribute instead.

Transaction management

These functions manage transaction boundaries, controlling when changes are committed or rolled back.

Function Description
SQLEndTran Commits or rolls back a transaction. Can apply to all connections on an environment or a single connection.
SQLTransact Commits or rolls back a transaction. Deprecated in ODBC 3.x; use SQLEndTran instead.