【发布时间】:2015-08-04 21:12:53
【问题描述】:
我正在尝试使用 ODBC 在存储过程中将表值参数作为参数传递。我遵循了 MSDN 中的示例,但在调用 SQLBindParameter 时收到以下错误:
HY004 [Microsoft][ODBC SQL Server Driver]无效的 SQL 数据类型
这是我的代码。
//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 上找到了这个:
表值参数列不能绑定为 SQL_SS_TABLE 类型。如果在 ParameterType 设置为 SQL_SS_TABLE 的情况下调用 SQLBindParameter,则返回 SQL_ERROR 并生成一条诊断记录,其中 SQLSTATE=HY004, "Invalid SQL data type"。 SQLSetDescField 和 SQLSetDescRec 也会发生这种情况。
但我正在尝试绑定表格项,而不是表格列。这几乎似乎直接与他们的代码示例中所述的内容相矛盾。我不确定为什么会发生此错误。有什么想法吗?
非常感谢。
【问题讨论】:
标签: c++ odbc sqlbindparameter