Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create CLR stored procedure with Nvarchar(max) parameter?

Is it possible to create CLR stored procedure in SQL Server CLR project having input parameterof type nvarchar(max)?

If you define stored procedure:

<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub MyProcedure(ByVal param1 As String)

Then when you deploy it, param1 is of type NVarchar(4000). Is there a way to have it NVarchar(max)?

like image 550
Konstantin Nizhegorodov Avatar asked Nov 18 '10 10:11

Konstantin Nizhegorodov


People also ask

How NVARCHAR Max is stored?

VARCHAR(MAX) or NVARCHAR(MAX) is considered as a 'large value type'. Large value types are usually stored 'out of row'. It means that the data row will have a pointer to another location where the 'large value' is stored...

Is NVARCHAR Max same as NVARCHAR 4000?

The answers is: there is no different between nvarchar(7) and nvarchar(4000) in term of performance & storage size. There is an interesting thing is that: if you change nvarchar(7) or nvarchar(4000) to nvarchar(max). There is a difference in term of performance & storage size.

How can I increase NVARCHAR max size in SQL?

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 Max in NVARCHAR?

n defines the string size in byte-pairs and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^30-1 characters (2 GB). The storage size is two times n bytes + 2 bytes.


2 Answers

You can use the SqlFacet attribute. If you want the NVARCHAR(MAX) type as a parameter, then you should do this:

[SqlProcedure]
public static void storedProcedure1([SqlFacet(MaxSize=-1)] String param){ .. }

If you need it as a return value in a user defined function:

[return:SqlFacet(MaxSize=-1)]
[SqlFunction]
public static String userFunction1(){ ... }

The MaxSize=-1 indicates that the size of the NVARCHAR will be MAX.

like image 141
Guillermo Gutiérrez Avatar answered Oct 16 '22 15:10

Guillermo Gutiérrez


Define your parameter to be of type SqlChars, instead of string. See Handling Large Object Parameters in the CLR

like image 25
Damien_The_Unbeliever Avatar answered Oct 16 '22 17:10

Damien_The_Unbeliever