Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting nvarchar length to maximum in table valued parameters

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

like image 700
Arianule Avatar asked Apr 07 '14 12:04

Arianule


People also ask

How can I increase Nvarchar max size in SQL Server?

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)[^]:

What is the maximum length of Nvarchar Max?

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.

What is the difference between Nvarchar 50 and Nvarchar Max?

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...

When should I use Nvarchar Max?

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.


1 Answers

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 ),
    ....
}
like image 168
Steve Avatar answered Oct 06 '22 02:10

Steve