我正在尝试使用 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/