Jonathan Wood
Jonathan Wood

Reputation: 67193

SQLBindParameter either says it needs more data or ignores my value

I have the following method to bind an int value to a SQL statement:

void BindIntColumn(int nColumn, int nValue)
{
    ASSERT(m_hStmt != SQL_NULL_HSTMT);
    SQLINTEGER nSize = sizeof(nValue);
    SQLRETURN nReturn = ::SQLBindParameter(m_hStmt, nColumn, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &nValue, 0, &nSize);
    COdbcException::CheckError(nReturn, m_hStmt, SQL_HANDLE_STMT);
}

When I run this code, ::SQLBindParameter() returns SQL_NEED_DATA. I don't know why.

If I change the last argument to ::SQLBindParameter() to nullptr, then it returns SQL_SUCCESS. And SQLExecute() does in fact add the row! But the value added for this column is some randomly large number, such as 13498810. (The value being passed to this method is 1.)

I also tried changing the nValue parameter to type long but got the same result.

Can anyone help me make sense of this? I just want to bind an integer value to my statement.

Upvotes: 0

Views: 273

Answers (1)

SoronelHaetir
SoronelHaetir

Reputation: 15164

The ParameterValuePtr parameter to SQLBindParameter is a deferred parameter, that is the value is stored and not used until you call SQLExecute or SQLExecDirect and it needs to be valid at that time. Same thing with the StrLen_or_IndPtr parameter (so long as it is not null). Here you pass the address of a local variable but it goes out of scope before the statement is executed.

Your error about SQL_NEED_DATA makes no sense as that is not documented as a legal return value from SQLBindParameter at all.

Upvotes: 0

Related Questions