Maximum length of dynamic sql in sql server 2008?

hi

Maximum length of dynamic sql in sql server 2008?

thanks and regards

samba ch

December 28th, 2012 9:41am

Hello samba,

sp_executesql accepts a nvarchar(max) datatype, so the max size can be up to 2 GB.

Free Windows Admin Tool Kit Click here and download it now
December 28th, 2012 9:51am

Hi,

There is no such limit

Declare the Sql query as VARCHAR(MAX)

eg:

DECLARE @sql AS VARCHAR(MAX)

Tthe maximum length for a VARCHAR(MAX) data type is 2,147,483,645.

December 28th, 2012 9:52am

Hello Samba,

   Adding to Olaf's comment , Refer to the below thread on various ways for executing the dynamic SQL for more than 8000 length

http://social.msdn.microsoft.com/Forums/en/sqldatabaseengine/thread/45be5ec4-2038-4d06-9831-cb18207a676b

Free Windows Admin Tool Kit Click here and download it now
December 28th, 2012 9:54am

Have a look at this link

http://msdn.microsoft.com/en-us/library/ms188001.aspx

sp_executesql [ @stmt = ] statement
[ 
  { , [ @params = ] N'@parameter_name data_type [ OUT | OUTPUT ][ ,...n ]' } 
     { , [ @param1 = ] 'value1' [ ,...n ] }
]
        
Arguments
December 28th, 2012 2:23pm

Hi there,

the variable you use to assign your Dynamic SQL string have limitations.

What is your variable data type. If you use varchar(max) or nvarchar(max) have limit of 2GB.

varchar holds Acscii and nvarchar holds non-ascscii character formats.

thanks

kumar

Free Windows Admin Tool Kit Click here and download it now
January 2nd, 2013 12:02am

Length of a string containing SQL statements (batch size)1:

  65,536 * Network packet size

1Network Packet Size is the size of the tabular data stream (TDS) packets used to communicate between applications and the relational Database Engine. The default packet size is 4 KB, and is controlled by the network packet size configuration option.

source: https://msdn.microsoft.com/en-us/library/ms143432%28v=sql.100%29.asp

PS: sorry for necroposting :) but this thread at top of google, so it need correct answer.

  • Edited by 71rmn 3 hours 50 minutes ago
May 18th, 2015 11:12pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics