SQLBindParameter to prepare for SQLPutData using C++ and SQL Native Client

I’m trying to use SQLBindParameter to prepare my driver for input via SQLPutData. The field in the database is a TEXT field. My function is crafted based on MS’s example here:

I’ve setup the environment, made the connection, and prepared my statement successfully but when I call SQLBindParam (using code below) it consistently fails reporting: [Microsoft][SQL Native Client]Invalid precision value

  • TSQL foreign keys on views?
  • SQL server wrong Row Number is showing
  • Convert Varchar Column to Datetime format - SQL Server
  • Copy table to a different database on a different SQL Server
  • Convert YYYYMM to MMMYY
  • SQL - Insert NULL into DateTime
  • int col_num = 1;
    SQLINTEGER length = very_long_string.length( );
    retcode = SQLBindParameter( StatementHandle,
                (SQLPOINTER) col_num,     
                &length ); 

    The above relies on the driver in use returning “N” for the SQL_NEED_LONG_DATA_LEN information type in SQLGetInfo. My driver returns “Y”. How do I bind so that I can use SQLPutData?

  • How to get top 'n' percentage values
  • Do I need to install SQL Server 2008 SP1 on the management studio express?
  • Convert C# classes (class library) to SQL DDL (tables)
  • T-SQL: sorting results by year, month
  • Does using (var connection = new SqlConnection(“ConnectionString”)) still close/dispose the connection on error?
  • how to create temp table while Joining multiple tables that have to be PIVOT
  • 2 Solutions collect form web for “SQLBindParameter to prepare for SQLPutData using C++ and SQL Native Client”

    you’re passing NULL as the buffer length, this is an in/out param that shoudl be the size of the col_num parameter. Also, you should pass a value for the ColumnSize or DecimalDigits parameters.


    Though it doesn’t look just like the documentation’s example code, I found the following solution to work for what I’m trying to accomplish. Thanks gbjbaanb for making me retest my input combinations to SQLBindParameter.

        SQLINTEGER length;
        RETCODE retcode = SQLBindParameter( StatementHandle,
            col_num,      // position of the parameter in the query
            data_length,        // size of our data
            NULL,               // decimal precision: not used our data types
            &my_string,         // SQLParamData will return this value later to indicate what data it's looking for so let's pass in the address of our std::string
            &length );          // it needs a length buffer
        // length in the following operation must still exist when SQLExecDirect or SQLExecute is called
        // in my code, I used a pointer on the heap for this.
        length = SQL_LEN_DATA_AT_EXEC( data_length ); 

    After a statement is executed, you can use SQLParamData to determine what data SQL wants you to send it as follows:

        std::string* my_string;
        // set string pointer to value given to SQLBindParameter
        retcode = SQLParamData( StatementHandle, (SQLPOINTER*) &my_string ); 

    Finally, use SQLPutData to send the contents of your string to SQL:

        // send data in chunks until everything is sent
        SQLINTEGER len;
        for ( int i(0); i < my_string->length( ); i += CHUNK_SIZE )
            std::string substr = my_string->substr( i, CHUNK_SIZE );
            len = substr.length( );
            retcode = SQLPutData( StatementHandle, (SQLPOINTER) substr.c_str( ), len );
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.