SQLDescribeParams函数调用 - SQL Server Native Client 11.0驱动程序失败并显示“无效的对象名称”

时间:2013-10-23 19:28:53

标签: c++ sql-server odbc sql-server-2012 sql-server-native-client

我想在SQL Server 2012上创建一个临时表并批量插入其中。我并不总是知道临时表将创建的参数类型,所以我需要调用SQLDescribeParam以便我可以绑定参数。从http://msdn.microsoft.com/en-us/library/ms710188(v=vs.85).aspx

复制SQLDescribeParam的示例代码后,我使用以下精简代码

下面的代码适用于针对SQL Server 2012的“SQL Server Native Client 10.0”ODBC驱动程序,我能够绑定参数并插入临时表。但SQLDescribeParam调用对于“SQL Server Native Client 11.0”驱动程序失败,并出现以下两个错误:

Error record 1:
Description: '[Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name '#myTemp'.'
SQL State: 42S02
Native Error: 208

Error record 2:
Description: '[Microsoft][SQL Server Native Client 11.0][SQL Server]The batch could not be analyzed because of compile errors.'
SQL State: 42000
Native Error: 11501

如何让SQLDescribeParam在“SQL Server Native Client 11.0”上运行,我需要做些什么?

我在Windows 7上使用64位“SQL Server Native Client”驱动程序。

代码如下:

SQLSMALLINT   NumParams, i, DataType, DecimalDigits, Nullable;
SQLULEN  ParamSize;
SQLWCHAR strCreateQuery[] = L"CREATE TABLE [#myTemp] ( mycol INT)";
SQLWCHAR strInsertQuery[] = L"INSERT INTO [#myTemp] ([mycol]) VALUES (?)";
SQLHSTMT hstmt;

{ // Create temp table #myTemp
// Allocate hstmt
SQLExecDirectW( hstmt, strCreateQuery, SQL_NTS );
// Deallocate hstmt
}

// Allocate hstmt
SQLPrepare(hstmt, strInsertQuery, SQL_NTS);
SQLNumParams(hstmt, &NumParams);

if (NumParams) {

for (i = 0; i < NumParams; i++) {
    // Describe the parameter.
    SQLDescribeParam(hstmt, i + 1, &DataType, &ParamSize, &DecimalDigits, &Nullable) );
}
}
// Deallocate hstmt

0 个答案:

没有答案