I'm using:
Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) Dec 28 2012 20:23:12
Copyright (c) Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 6.2 (Build 9200: ) (Hypervisor)
I have a stored procedure which has a varchar(MAX)
output parameter.
I'm calling it via an OdbcCommand
and I've created a VarChar
output parameter and added it to the OdbcCommand
Parameters
list:
new OdbcParameter("@MaxField", OdbcType.VarChar)
{ Direction = ParameterDirection.Output, Size = -1 })
While debugging I can see that the Size
field on the parameter is still -1 but when I run the query using OdbcCommand.ExecuteNonQuery()
I get the following exception:
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=String[2]: the Size property has an invalid size of 0.
Source=System.Data
StackTrace:
at System.Data.Odbc.OdbcParameter.GetParameterSize(Object value, Int32 offset, Int32 ordinal)
at System.Data.Odbc.OdbcParameter.PrepareForBind(OdbcCommand command, Int16 ordinal, Int32& parameterBufferSize)
at System.Data.Odbc.OdbcParameterCollection.CalcParameterBufferSize(OdbcCommand command)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader)
at System.Data.Odbc.OdbcCommand.ExecuteNonQuery()
I tried using the OdbcCommandBuilder.DeriveParameters(odbcCommand);
to examine the parameter information derived from the stored procedure and it said that the parameter had a Size of 2147483647 - however, if I try and use that value I get a different exception telling me
System.Data.Odbc.OdbcException was unhandled
ErrorCode=-2146232009
HResult=-2146232009
Message=ERROR [42000] [Microsoft][ODBC SQLServer Driver][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. Source=SQLSRV32.DLL StackTrace:
at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader)
at System.Data.Odbc.OdbcCommand.ExecuteNonQuery()
How should I configure my OdbcCommand output parameter to receive this output from the stored procedure?
This is on an existing, in-production database so trying to change the datatype of the field is sadly the absolute last resort. The field that is being selected is varchar(max)
.
Edit: If the actual answer is "You just can't" then that would be acceptable - at least I would know for sure.