I have defined a stored procedure that has a parameter of type NVARCHAR(max)
and I process that string in my stored procedure
. I know that max of nvarchar
is 4000. But I have passed a string with 5700 characters to my sp with no errors. Is it possible?

- 22,810
- 38
- 143
- 225
-
NVARCHAR(MAX) can store more than 2 GB of data .Kindly refer this answer http://stackoverflow.com/questions/12639948/sql-nvarchar-and-varchar-limits – praveen Dec 25 '12 at 12:07
-
According to [this](http://msdn.microsoft.com/en-us/library/ms186939.aspx) nvarchar [ ( n | max ) ] Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000 – Bob Dec 25 '12 at 12:08
-
5@breceivemail: if you read on the page you linked to, it also says: *max indicates that the maximum storage size is 2^31-1 bytes (2 GB).* – marc_s Dec 25 '12 at 12:09
3 Answers
Yes it is possible - according to the MSDN documentation:
nvarchar [ ( n | max ) ]
Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB). The storage size, in bytes, is two times the actual length of data entered + 2 bytes. The ISO synonyms for nvarchar are national char varying and national character varying.
So if you specify nvarchar(max)
you can store up to 1 billion 2-byte Unicode characters. That's Leo Tolstoj's War and Peace well over a hundred times over ....
SQL Server stores those max columns into special structures internally, which makes it possible to get around the 8K limit of the SQL Server pages. It works - but it's more effort than just storing a few hundred bytes on a page, so this storage system does pose more strain on SQL Server - use it with care, use it only when you really need to (and most definitely don't just make all your columns (n)varchar(max)
, just because you're lazy!)
Check out this really good article on Simple Talk: What's the Point of Using VARCHAR(n) Anymore? - it explains very nicely how (max)
datatypes are different and less suited for smaller strings - use only when really needed!

- 732,580
- 175
- 1,330
- 1,459
You Could Try
Declare @SQL as nvarchar(Max)
,@SQL2 as nvarchar(Max)
-- Just Sample
-- each variable max 4000 character, so be wise in splitting your syntax
Set @SQL = 'Select * '
Set @SQL2 = 'From table A'
EXEC (@SQL+@SQL2)
Note if your dynamic string length more than > 8000 character you should split into 3 variables'
this should works, because i was have same problem

- 1,759
- 5
- 12
- 18

- 11
- 1
If you are concating your string then you have to concate it like below.
DECLARE @qry NVARCHAR(MAX);
SET @qry = N'SELECT * FROM Table1 t1';
SET @qry = @qry + N'INNER JOIN Table2 t2 ON t1.Col1 = t2.Col2'
SET @qry = @qry + N'INNER JOIN Table3 t3 ON t1.Col1 = t2.Col3'
EXEC sp_executeSQL @qry
In above example even if the characters of the concated string exceeds 4000 characters then also it will be added into the variable and the limit is upto 2GB.

- 355
- 1
- 8
- 27