Question

The SQLBindCol function expects a buffer length (for certain buffer types, like strings):

http://msdn.microsoft.com/en-us/library/ms710118(v=vs.85).aspx

SQLRETURN SQLBindCol(
      SQLHSTMT       StatementHandle,
      SQLUSMALLINT   ColumnNumber,
      SQLSMALLINT    TargetType,
      SQLPOINTER     TargetValuePtr,
      SQLLEN         BufferLength,
      SQLLEN *       StrLen_or_Ind);

How do I know how much bytes I should allocate?

The only thing I can think of is getting the SQL_DESC_LENGTH using SQLGetDescField, but I would have to execute the query twice then.

Was it helpful?

Solution

As it turns out I confused the order in which the functions are run, the correct order is:

  1. SQLBindParameter
  2. SQLExecDirect
  3. SQLBindCol

For SQLBindParameter (at least for input parameters) the lengths are already known before the statement is executed.

After SQLExecDirect you can use the SQLNumResultCols and SQLGetDescField functions to determine the number of columns, their data types, lengths, etc.

The SQL92/CLI (call level interface) documentation has an example in appendix B.2 Interactive Query that uses SQLDescribeCol to get the length.

OTHER TIPS

Call SQLDescribeCol before binding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top