how to set 9000 characters in nvarchar(max) - sql-server

I need to set 9000 characters in nvarchar variable
declare #inXMLRequest xml
declare #insertsql nvarchar(max)
set #insertsql='--------9000 characters--------'
EXEC sp_executesql #insertsql,
N'#inXMLRequest XML OUTPUT',
#inXMLRequest OUTPUT
print #insertsql
But NVARCHAR is taking 5000 characters only
how to set 9000 characters in NVARCHAR variable?

if you write a small program that tries the same thing outside the management studio you will be able to do this.
unfortunately for some reason it is not possible to do this with a insert statement in the management studio. there is a 4k limit on what you put into the management studio.
I've people do inserts before, ie, first insert 4k chars then the next 4k chars etc

I'm not sure, but you cant store such long rows in varchar.
a row have to fit into a db-page.
varchar fields are stored in the same page, where the row is stored.
a page is usually smaller than 8192 bytes (depending on db-system)
there a some exceptions like blob fields which are not stored in the same page of a row.
you should use a other data type like text/blob/image.
nvarchar 9000 is btw. impossible this will allocate 18000 byte which dont fit into a page.
this is the reason for the 4000 char limit.

Thanks to All,
Insted of using SP_Executesql directly we Executing nvarchar variable
Above we Are preparing #insertsql nvarchar variable morethan 8000 characters and it is giving to sp_executesql like this
EXEC sp_executesql #insertsql, N'#inXMLRequest XML OUTPUT',#inXMLRequest OUTPUT
insted of above query replaced with below query
Exec ('DeClare #inXMLRequest XML SET #inXMLRequest='------above 8000 characters---')
Finally we will execute that nvarchar string and get out put

Related

Dynamic SQL Statement is too long

I am using quite a long dynamic SQL Statement (a bit more than 13000 characters) but when I am trying to execute it, I am noticing that the exec isn't reading the statement completly and cuts the last part off.
I am using:
DECLARE #Statement nvarchar(max)
SET #Statement = N'[LONG STATEMENT]'
EXEC (#Statement)
I did notice, that it could read even less characters, if I am not using the the brackets in EXEC (#Statement)
I also tried using EXEC sp_executesql #Statement
It just stops reading the statement after 12482 characters...
I have the problems with SQL-Server 2008 R2 and SQL Server 2014
EDIT: OK, now I noticed something different. It seems, that the lenght itself of the statement is not exactly the problem. As I mentioned in a comment below, I am using this long dynamic sql statement, because I am creating an update script, which adds a new stored procedure and within this procedure I am using table names, which can differ. So I created variables, which contain the table names and used these variables with the dynamic sql statement, so I don't need to change the table names within the procedures and functions I am adding with this update script, but just changing the content of the variables.
However, if I am NOT using these variables and use the table names "hardcoded" in the statement, the statement can be executed successfully...
I guess the answer is here:
So I created variables, which contain the table names and used these
variables with the dynamic sql statement, so I don't need to change
the table names within the procedures and functions I am adding with
this update script, but just changing the content of the variables.
I guess, your dynamic T-SQL statement is built using string concatenation. So, let's say we have something like this:
DECLARE #DynamicSQLSTatement NVARCHAR(MAX);
DECLARE #TableName01 NVARCHAR(128) = 'T01';
DECLARE #TableName02 NVARCHAR(128) = 'T02';
DECLARE #TSQL NVARCHAR(4000) = REPLICATE(N'X', 4000);
SET #DynamicSQLSTatement = #TableName01 + #TSQL + #TableName02;
We have three short strings (length < max) and when they are concatenated, we expect that the new NVARCHAR(MAX) value will be capable of storing the whole new string (it is with max length, after all).
So, the following statement will give as T02, right?
SELECT RIGHT(#DynamicSQLSTatement, 3);
But no, the output is XXX. So, the question is why the whole concatenation text is not preserved?
When you are concatenating nvarchar(1-4000) strings they output string is not converted to max if it is not possible to store all the data.
In order to fix this, we can cast the first part of the string to nvarchar(max):
SET #DynamicSQLSTatement = CAST(#TableName01 AS NVARCHAR(MAX)) + #TSQL + #TableName02
SELECT RIGHT(#DynamicSQLSTatement, 3);
I'd imagine your hitting a limit of about 16K (8K used to be the most data you could hold in a variable before nvarchar(max) etc was invented. You could try using varchar(max) instead of nvarchar as that will be half the size (unicode is 16-bit, ascii is 8-bit). I have a feeling that won't help much though.
Really though, you're hitting this issue because whatever you're trying to do shouldn't really be done like that, super long SQL statements are a sign that you've gone down the wrong path. Find a way to break down your query or operations. If this is a query, consider if you could restrict your data set over a few steps rather than in one query.

DataType to store characters of length 10000 in SQL Server 2008

DataType to store characters of length 10000 in SQL Server 2008 , I tried using VARCHAR(MAX) , also nVARCHAR(MAX) none are of help.
I am declaring one output variable in dynamicSP, which I want to be of size greater than 10k characters.
Thanks!
We had the same problem. Our solution is store the query in a variable as a text. then concatenate them until the query ended.
Example:
DECLARE #sql AS NVARCHAR(MAX)
SET #sql = 'query here'
#sql=#sql+'query here'
exec(#sql)

using TYPE (via sqlserver 2008) command limits buffer size to 255

I am using master.dbo.xp_cmdshell to load txt/csv file to SQL server, in the following way:
CREATE TABLE #tempoutput
(
result_id [int] IDENTITY(1,1) NOT NULL,
result VARCHAR(MAX)
)
INSERT INTO #tempoutput(result)
EXEC #rcode = master.dbo.xp_cmdshell TYPE c:\test.csv
the thing is that although result column is defined as VARCHAR(MAX), when I try to take header with:
SELECT top 1 #result=result
FROM #Tempoutput
I can only fetch 255 characters, any reason why? and how to be able to fetch all row till CRLF
Thanks!
The result does contain all the data, its just broken into multiple rows of 255 chars in your #tempoutput table. (Remove the TOP 1 to see all the rows)
INSERT INTO #tempoutput(result)
EXEC #rcode = master.dbo.xp_cmdshell "TYPE c:\ReallyBig.csv"
SELECT * FROM #tempoutput; -- You'll see the extra rows
If you need to reassemble all lines back into a single VARCHAR(MAX), you'll need to use your favourite GROUP_CONCAT hack. Since you're already using xp_cmdshell, you probably have unlimited access to your server, so why not install a CLR aggregate function like this with example usage to do just this - IMO this is much more readable than the FOR XML ... STUFF hacks.

Execute very long statements in TSQL using sp_executesql

I would like to execute dynamic SQL statements which are about 10,000 characters.
When I use sp_executesql as below:
DECLARE #stmt varchar(MAX)
SET #stmt = 'xxxxxxxx.................' which is about 10,000 characters
EXEC sp_executesql #stmt
I got the following error
The character string that starts with ' select t1.e_reference xxxxxxxxxxx' is too long. Maximum length is 8000.
As far as I know, we can use sp_executesql to execute very long statements, can't we?
I am using SQL Server 2008, Enterprise Edition, 64 bit.
How can I achieve this? Thanks.
Based on your responses in the post, you are using linked server.
The 8000 char limit is not posed by sp_executesql, but by OPENQUERY that you are probably using in your variable #stmt .
MSDN says this of OPENQUERY's arguments:
'query' Is the query string executed in the linked server. The maximum
length of the string is 8 KB.
http://msdn.microsoft.com/en-us/library/ms188427.aspx
To bypass this, you could probably use
execute (#query) at oracle_linked_server
MSDN says this which is a bit vague:
"The size of the string is limited only by available database server memory. On 64-bit servers, the size of the string is limited to 2 GB, the maximum size of nvarchar(max)."
On 64-bit servers the limit is 2GB. It is not clear what is the limit of 32-bit servers? Is it 4000, 8000, whatever memory is available, 2GB?
The #stmt parameter for sp_executesql has a data type of nvarchar(8000), so you have exceeded the limit.
Either refactor your SQL statements into smaller parts, or put the SQL into a stored procedure.

How big should I declare my SQL Server column if it's going to hold an Article body?

I'm going to save tutorials and snippets. How big should I declare the column?
Declare as varchar(max) (if the size will exceed 8,000 bytes). (or nvarchar(max) if supporting multi-byte character sets)
varchar(max) or nvarchar(max)
[TEXT(MAX) or NTEXT(MAX) are being deprecated in future versions.]
If you mean what datatype, probably you need nvarchar(max). If you are still on SQL Server 200 you need ntext or text.

Resources