Search code examples
sql-server-2008nvarcharodbc

SqlBindParameter Invalid precision value on a nvarchar(max) column


I have a table that has a column ([To]) that is defined as nvarchar(max). According to http://msdn.microsoft.com/en-gb/library/ms186939.aspx the maximum length of an nvarchar(max) column is 2^32.

I am trying to bind a string > 4000 characters (I am guessing the threshold for success is 4000, but I haven't actually proved that) to this column and its failing with HY104:1:0

SQLBindParameter:HY104:1:0:[Microsoft][SQL Server Native Client 11.0]Invalid precision value

An example length of string I am trying to bind is 21707.

I have tried running using the following ODBC drivers and the result is the same for all of them:

{SQL Server}
{SQL Server Native Client 10.0}
{SQL Server Native Client 11.0}

The actual call I am making is:-

Q->ret = SQLBindParameter(Q->stmt, i, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_WVARCHAR, 
    (SQLINTEGER) (len ? len : 1), 0, data, (SQLINTEGER) len, 0);

Where:-

len = 21707
data = unicode string of length 21708 characters (43414 bytes) followed by (WORD)0

SQL Server Details:

Microsoft SQL Server Express Edition with Advanced Services (64-bit)
10.50.2550.0
Windows 7 64 bit

What am I doing wrong?


Solution

  • The answer is, I need to use SQL_WLONGVARCHAR not SQL_WVARCHAR when binding a long string.