Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many characters long may an SQL Server SQL statement be?

What is the maximum length of an SQL statement in SQL Server? Does this length depend on the version of SQL Server?

For example, in

DECLARE @SQLStatement NVARCHAR(MAX) = N'Something' 
EXEC(@SQLStatement) 

the @SQLStatement is allowed to be X characters long. What is X?

like image 406
Soheil Farahani Avatar asked Feb 08 '12 21:02

Soheil Farahani


People also ask

How long can SQL statements be?

The maximum standard SQL query length is 1024.00K characters, including comments.

How do I store more than 8000 characters in SQL Server?

You still Cannot have a Single Unbroken Literal String Larger than 8000 (or 4000 for nVarChar). Literal Strings are those you hard-code and wrap in apostrophe's. You must Break those Strings up or SQL Server will Truncate each one BEFORE concatenating. I add ' + ' every 20 lines (or so) to make sure I do not go over.

What is Max length of varchar in SQL Server?

varchar [ ( n | max ) ] Variable-size string data. Use n to define the string size in bytes and can be a value from 1 through 8,000 or use max to indicate a column constraint size up to a maximum storage of 2^31-1 bytes (2 GB).

What is the limit of SQL Server?

SQL Server allows a maximum of 32,767 user connections.


2 Answers

It's going to be 65,536 times your network packet size, according to the documentation

Seems like this is the same at least since SQL Server 2005

like image 76
Eric Petroelje Avatar answered Oct 05 '22 03:10

Eric Petroelje


Verbatim from the documentation:

65,536 * Network Packet Size

What isn't clear, but which any sane person would immediately question, is what does "Network Packet Size" mean?

Network packet size, in this case, as expected, does not refer to physical size, since this would break the abstraction of end-to-end guaranteed delivery streaming protocol (TCP), but rather refers to packet size in the context of the Tabular Data Stream protocol used by SQL Server for communication. According to the same documentation, the default size of a TDS packet is 4KB.

like image 31
Eric Smith Avatar answered Oct 05 '22 03:10

Eric Smith