18

I want to pass a table valued parameter as a variable to a stored procedure and in the constructor of class SqlMetadata one can specify the length (long maxLength) of the string one wants to add in the column of the table.

Microsoft.SqlServer.Server.SqlMetaData[] tvpdefinition = 
        {

            new SqlMetaData("ValueOne", SqlDbType.NVarChar, 100),
            new SqlMetaData("ValueTwo",SqlDbType.NVarChar, 100)
        }

How can one go about specifying a 'max' length so that it corresponds with this column

ValueOne (nvarchar(max), not null)

as opposed to a length value of 100 for example

cf_en
  • 1,661
  • 1
  • 10
  • 18
Arianule
  • 8,811
  • 45
  • 116
  • 174

1 Answers1

26

In this article on MSDN it is specified that you can set the MAX size in this way

SqlParameter myParam = new SqlParameter("@paramName", SqlDbType.NVarChar, SqlMetaData.Max );

See the last example on the above mentioned article.
So, without knowing exactly how your SqlMetaData class is defined, and supposing that the last parameter is the size propery of an underlying SqlParameter, I think you could write

Microsoft.SqlServer.Server.SqlMetaData[] tvpdefinition = 
{
    new SqlMetaData("ValueOne", SqlDbType.NVarChar, SqlMetaData.Max ),
    ....
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    I just checked that now as well...should have checked before asking. Apologies – Arianule Apr 07 '14 at 13:01
  • 1
    No apologies are required when no offense has been taken. On the contrary yours is an interesting question, and it has been a valuable way to improve my knowledge. – Steve Apr 07 '14 at 13:05
  • 4
    BTW, you can use `SqlMetaData.Max` instead of -1, which might be slightly cleaner-looking. – Branko Dimitrijevic Jul 25 '17 at 10:20