all groups > sql server odbc > march 2007 >
You're in the

sql server odbc

group:

How do I bind a NULL string with SQLBindParameter?


How do I bind a NULL string with SQLBindParameter? ()z
3/2/2007 9:36:25 AM
sql server odbc:
I have a routine that binds a string to a query. It is passed a
string and a length or NULL and 0. It works find for non-null strings
but I cant get the handling of NULL to work.

void bindString(struct _query *Q, int i, void *data, size_t len) {
LOG("BIND STRING PARAM %d ADDR %lx LEN %d\n", i, data, len);
if (data) {
Q->ret = SQLBindParameter(Q->stmt, i, SQL_PARAM_INPUT, SQL_C_CHAR,
SQL_VARCHAR, (SQLINTEGER) len, 0, data, (SQLINTEGER) len, 0);
} else {
SQLLEN ind = SQL_NULL_DATA;
Q->ret = SQLBindParameter(Q->stmt, i, SQL_PARAM_INPUT, SQL_C_CHAR,
SQL_VARCHAR, 0, 0, (SQLPOINTER)1, 0, &ind);
// This errors with Invalid Precision Value.
}
if (!SQL_SUCCEEDED(Q->ret)) {
DUMP_DIAGNOSTICS("SQLBindParameter", Q->stmt, SQL_HANDLE_STMT);
}
}

Why do I have to pass a length when I am trying to bind a null
parameter? I dont know the length because my string is NULL, it does
not have a length.

How do I change:

Q->ret = SQLBindParameter(Q->stmt, i, SQL_PARAM_INPUT, SQL_C_CHAR,
SQL_VARCHAR, 0, 0, (SQLPOINTER)1, 0, &ind);

to get it to work.

Thanks
Re: How do I bind a NULL string with SQLBindParameter? ()z
3/2/2007 10:05:33 AM
[quoted text, click to view]

Got it working. There is two things wrong with this code.

1. SQLLEN ind is a local and goes out of scope by the time SQLExecute
is calls.
2. Passing 1 for precision, and 0 for data works, i.e.

static SQLLEN ind = SQL_NULL_DATA;
Q->ret = SQLBindParameter(Q->stmt, i, SQL_PARAM_INPUT,
SQL_C_CHAR,
SQL_VARCHAR, 1, 0, 0, 0, &ind);

Now, I dont like static variables so I will re-code this so that ind
is passed to this routine and remains in scope while the SQLExecute is
called.
AddThis Social Bookmark Button