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
Solution 2. Use navarchar(max) , which has a limit of 231-1 bytes (2 GB). Avoid the old ntext type, which has been deprecated for many years, and will be removed from a future version of SQL Server. ntext, text, and image (Transact-SQL)[^]:
You must specify max of the NVARCHAR column. The size of this parameter cannot exceed 255 bytes. When you place an index on an NVARCHAR column, the maximum size is 254 bytes. You can store shorter, but not longer, character strings than the value that you specify.
nvarchar max is for columns up to 2GB. So essentially it takes up more resources. You are better off using the nvarchar(50) if you know you aren't going to need that much space. each character is about 2 bytes so with 2 GB thats 1 billion characters...
Use nvarchar when the sizes of the column data entries vary considerably. Use nvarchar(max) when the sizes of the column data entries vary considerably, and the string length might exceed 4,000 byte-pairs.
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 ),
....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With