OdbcParameter error: "Data type 0x23 is a deprecated large object, or LOB, but is marked as output parameter."
Customer reported a bug for error in OdbcParameter. Customer was trying to get a VARCHAR(8000) type return value (output) from SQL Server stored procedure by OdbcCommand, and got exception thrown with error message:
[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid parameter 2 (''):
Data type 0x23 is a deprecated large object, or LOB, but is marked as output parameter.
Deprecated types are not supported as output parameters. Use current large object types instead.
Customer was not able to receive any string output longer than 3,999 characters from the stored procedure.
<Quotes from sample code>
OdbcCommand command = new OdbcCommand(sql, con, trn);
command.Parameters.Clear();
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@v1", OdbcType.VarChar, 8000); //Input
//Output. The error will not occur when 4000 is 3999.
command.Parameters.Add("@v2", OdbcType.VarChar, 4000);
command.Parameters.Add("@v3", OdbcType.Int);
command.Parameters.Add("@v4", OdbcType.Int);
command.Parameters["@v1"].Direction = ParameterDirection.Input;
command.Parameters["@v2"].Direction = ParameterDirection.Output;
<Quotes from the StoredProcedure>
CREATE PROCEDURE testCSProc1 (
@v1 VARCHAR(8000),
@v2 VARCHAR(8000) OUT,
@v3 INTEGER OUT,
@v4 INTEGER OUT
)
AS
BEGIN
SET @v2 = @v1 + @v1;
SET @v3 = datalength(@v1);
SET @v4 = datalength(@v2);
END;
GO
This is in System.Data.Odbc, right? Do we need a label specific for that?
@danmosemsft Thanks for the pointing out the label. Yes it is System.Data.Odbc, and I changed the label to System.Data since we do not have Odbc specific label.
It works only up to 3,999 characters for output. The reason is the bind-type VARCHAR is changed to TEXT(LONGVARCHAR) when the output string length configured is longer or equal to 4,000 characters. The bind-type was supposed to be remained as VARCHAR up to 8,000 characters long for output as shown in design spec.
Here is the code spot that cause this error: https://github.com/dotnet/corefx/blob/e7723074a7ee7cfbc1da516108d2ad85082d77d4/src/System.Data.Odbc/src/System/Data/Odbc/OdbcParameter.cs#L752-L771
cbParameterSize is the output string buffer size in bytes on client side. 2 bytes are assigned to each character by GetParameterSize(value, offset, ordinal) method. Single 2 bytes are assigned for null terminator at the end as indicator.
Therefore, buffer size for 3,999 length output string will be (3999 * 2) + 2 = 8000 bytes, which number is the max size that can make the if-condition false and avoid the bind-type change. 4,000 character length will make output buffer size (4000 * 2) + 2 = 8002 bytes, and it will make the if-condition true and cause bind-type change to TEXT(LONGVARCHAR).
Here is the erroneous if-statement in line#763:
if ((cbParameterSize > 8000))
{
_bindtype = TypeMap._Text; // will change to LONGVARCHAR
}
I also found that another similar error would happen when customer uses NVARCHAR for output value type. NVARCHAR supports up to 4,000 characters in design spec, but actually it only supports 1,999 character length in reality when it is used as output type. Output string length configured that is longer or equal to 2,000 characters in NVARCHAR will cause similar error exception. The only difference in error message is the hex number of data type (0x63)
[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid parameter 2 (''):
Data type 0x63 is a deprecated large object, or LOB, but is marked as output parameter.
Deprecated types are not supported as output parameters. Use current large object types instead.
This sibling error is caused by this code spot in line#768:
if ((cbParameterSize > 4000))
{
bindtype = TypeMap._NText; // will change to WLONGVARCHAR
}
This second bug also can be reproduced by simply modifying the sample code the user provided:
[StoredProcedure.sql]
USE sampleDB
IF OBJECT_ID('testCSProc1', 'P') IS NOT NULL
DROP PROCEDURE testCSProc1;
GO
CREATE PROCEDURE testCSProc1 (
@v1 NVARCHAR(4000),
@v2 NVARCHAR(4000) OUT,
@v3 INTEGER OUT,
@v4 INTEGER OUT
)
AS
BEGIN
SET @v2 = @v1 + @v1;
SET @v3 = datalength(@v1);
SET @v4 = datalength(@v2);
END;
GO
[DbAccessor.cs]
public void callProc(string sql, string v1, string v2, int v3, int v4)
{
try
{
if (con == null || con.State != ConnectionState.Open)
{
printLog("Connection status error: ConnectionState -- " + con.State.ToString());
}
OdbcCommand command = new OdbcCommand(sql, con, trn);
command.Parameters.Clear();
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@v1", OdbcType.NVarChar, 4000); // Input
command.Parameters.Add("@v2", OdbcType.NVarChar, 2000); // Output. The error will not occur when 2000 is 1999
...
[Program.cs]
string clobstr = "";
for (int i = 0; i < 400; i++)
{
clobstr += "0123456789";
}
string v1 = clobstr;
string v2 = "";
Sample code for repro provided by customer: OdbcParameter error repro.zip
Fix was merged to main branch.