执行查询 (ODBC)Executing Queries (ODBC)

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance 是Azure Synapse AnalyticsAzure Synapse AnalyticsyesAzure Synapse AnalyticsAzure Synapse Analytics 是并行数据仓库Parallel Data Warehouseyes并行数据仓库Parallel Data Warehouse适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database 是Azure SQL 托管实例Azure SQL Managed InstanceYesAzure SQL 托管实例Azure SQL Managed Instance 是Azure Synapse AnalyticsAzure Synapse AnalyticsyesAzure Synapse AnalyticsAzure Synapse Analytics 是并行数据仓库Parallel Data Warehouseyes并行数据仓库Parallel Data Warehouse

在 ODBC 应用程序初始化连接句柄并与数据源连接后,它为连接句柄分配一个或多个语句句柄。After an ODBC application initializes a connection handle and connects with a data source, it allocates one or more statement handles on the connection handle. 然后,应用程序可以 MicrosoftMicrosoft SQL ServerSQL Server 对语句句柄执行语句。The application can then execute MicrosoftMicrosoft SQL ServerSQL Server statements on the statement handle. 执行 SQL 语句时的一般事件顺序为:The general sequence of events in executing an SQL statement is:

  1. 设置所有所需的语句属性。Set any required statement attributes.

  2. 构造语句。Construct the statement.

  3. 执行语句。Execute the statement.

  4. 检索任何结果集。Retrieve any result sets.

应用程序检索 SQL 语句所返回的所有结果集中的所有行后,它可以在同一语句句柄上执行其他查询。After an application retrieves all the rows in all of the result sets returned by the SQL statement, it can execute another query on the same statement handle. 如果应用程序确定不需要检索特定结果集中的所有行,则可以通过调用 SQLMoreResultsSQLCloseCursor来取消结果集的其余部分。If an application determines that it is not required to retrieve all the rows in a particular result set, it can cancel the rest of the result set by calling either SQLMoreResults or SQLCloseCursor.

如果在 ODBC 应用程序中必须使用不同数据多次执行同一 SQL 语句,可以在 SQL 语句的构造中使用用问号 (?) 表示的参数标记:If, in an ODBC application, you must execute the same SQL statement multiple times with different data, use a parameter marker denoted by a question mark (?) in the construction of an SQL statement:

INSERT INTO MyTable VALUES (?, ?, ?)  

然后,可以通过调用 SQLBindParameter将每个参数标记绑定到程序变量。Each parameter marker can then be bound to a program variable by calling SQLBindParameter.

执行所有 SQL 语句并处理它们的结果集之后,应用程序释放语句句柄。After all SQL statements execute and their result sets process, the application frees the statement handle.

SQL ServerSQL ServerNative CLIENT ODBC 驱动程序支持每个连接句柄多个语句句柄。The SQL ServerSQL Server Native Client ODBC driver supports multiple statement handles per connection handle. 在连接级别管理事务,以便将针对单个连接句柄上的所有语句句柄执行的所有工作视为同一事务的一部分来管理。Transactions are managed at the connection level, so that all work performed on all statement handles on a single connection handle are managed as part of the same transaction.

本节内容In This Section

另请参阅See Also

SQL Server Native Client (ODBC)SQL Server Native Client (ODBC)