1

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.

5
  • Try it with nvarchar(max), it takes a nvarchar parameter, so your varchar is having to be converted, slim chance that is causing the problem
    – Andrew
    Nov 16, 2011 at 11:50
  • I have tried it with nvarchar(MAX) as well. But it doesn't work.
    – TTCG
    Nov 16, 2011 at 11:56
  • 2
    Can you show more of your actual query? I can't reproduce this. In fact I can't even see that error in sys.messages. Are you querying a linked server? Nov 16, 2011 at 13:05
  • Yes, Martin. I am querying against Oracle Linked Server.
    – TTCG
    Nov 16, 2011 at 14:09
  • The linked server part is kind of important information, well done to Martin for picking up on it
    – Andrew
    Nov 17, 2011 at 14:37

4 Answers 4

8

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
0

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?

0

Break the code up into strings of less than 4k characters, then replace single quotes with double quotes, lastly execute like below.

SET @STM1 = REPLACE( @STM1, CHAR(39), CHAR(39) + CHAR(39);
SET @STM2 = REPLACE( @STM2, CHAR(39), CHAR(39) + CHAR(39); 
SET @STM3 = REPLACE( @STM3, CHAR(39), CHAR(39) + CHAR(39);
EXECUTE( N'EXECUTE sp_executesql N''' + @STM1 + N' ' + @STM2 + N' ' + @STM3 + '''' );

-1

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.

2

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.