c++ - 使用 ODBC、C++ 时绑定(bind)表值参数时出错

标签 c++ odbc

我正在尝试使用 ODBC 将表值参数作为存储过程中的参数传递。我遵循了 MSDN 中的示例,但在调用 SQLBindParameter 时收到以下错误:

HY004 [Microsoft][ODBC SQL Server Driver]Invalid SQL data type

这是我的代码。

//Allocate stament handle
SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt);

//Prep command
SQLPrepare(hStmt, (SQLCHAR*)"{call myStoredProc(?)}", SQL_NTS)

//Variables
const int arraySize = 2;
const int varcharSize = 30;
SQLCHAR *myUserDefTableName = (SQLCHAR *) "myUserDefTableName";
SQLLEN     myUserDefTableInd = 0;



//bind table item

int result = SQLBindParameter(hStmt, 
  1,// ParameterNumber
  SQL_PARAM_INPUT,// InputOutputType
  SQL_C_DEFAULT,// ValueType 
  SQL_SS_TABLE,// Parametertype
  (SQLINTEGER)arraySize,// ColumnSize - for a TVP this the row array size
  0,// DecimalDigits - for a TVP this is the number of columns in the TVP 
  (SQLPOINTER)myUserDefTableName,// ParameterValuePtr - for a TVP this is       the type name of the TVP
  SQL_NTS,// BufferLength - for a TVP this is the length of the type name or SQL_NTS
  &myUserDefTableInd);// StrLen_or_IndPtr - for a TVP this is the number of rows available

//bind columns for the table-valued parameter 
//and execute command
...

我还在 MSDN 上找到了这个:

A table-valued parameter column cannot be bound as type SQL_SS_TABLE. If SQLBindParameter is called with ParameterType set to SQL_SS_TABLE, SQL_ERROR is returned and a diagnostic record is generated with SQLSTATE=HY004, "Invalid SQL data type". This can also occur with SQLSetDescField and SQLSetDescRec.

但我正在尝试绑定(bind)表项,而不是表列。这似乎与他们的代码示例中所述的内容几乎直接矛盾。我不确定为什么会出现此错误。有什么想法吗?

非常感谢。

最佳答案

您确定将 ODBC 设置为版本 3.x 吗?在调用后立即尝试此操作以获取语句句柄:

 SQLHENV  hEnv  = SQL_NULL_HENV;
 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv); 

 SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);

关于c++ - 使用 ODBC、C++ 时绑定(bind)表值参数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30403601/

相关文章:

c++ - 最近的邻居用nanoflann搜索

c++ - 在 C++ 中, vector 是按值传递还是通过引用传递给函数

c++ - 从字符串中读取(可能使用数组?)C++

sql - ODBC SQL Server 驱动程序 - 从字符串转换日期和/或时间时转换失败

java - 无法使用 Windows-64 位连接到 MS Access DB

asp.net - 无需在系统上安装 MySQL ODBC 5.1 驱动程序即可运行 .net 网站

c++ - 读取文件时缓冲区大小会影响时间成本吗?

c++ - 请解释一下下面的函数

c# - 连接必须有效且打开(再次)

php - ODBC,PHP while 循环将数据分配给数组