Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: What's the limit on number of UNIONs?

I was wondering if there's a limit on a number of UNION'ed SELECT statements I can send to an SQL Server? I may have up to 36 done in one SQL statement, so it becomes quite long, character wise. (Also each of those SELECT statements is a somewhat complex by itself with CASE WHEN statements in it.)

like image 324
ahmd0 Avatar asked Jul 13 '11 09:07

ahmd0


People also ask

Is there a limit to union in SQL?

You probably confused “union all” with “cross join”. There are no general limits on SQL statement length, but there can be restrictions in the specific DBMS.

Can we use 3 Union in SQL?

Conclusion. Combining several tables to one large table is possible in all 3 ways. As we have seen, the behavior of UNION in SQL Server and UNION in DAX within Power BI is very similar.

Do unions slow down SQL?

Save this question. Show activity on this post. While each of both select statements takes less than 1 second when executed separately.

Does SQL Server have limit?

TIP: SELECT LIMIT is not supported in all SQL databases. For databases such as SQL Server or MSAccess, use the SELECT TOP statement to limit your results. The SELECT TOP statement is Microsoft's proprietary equivalent to the SELECT LIMIT statement.


1 Answers

From SQL 2008 BOL

"Any number of UNION operators can appear in a Transact-SQL statement"

Also from MSDN "Maximum Capacity Specifications for SQL Server" (2008):

Batch size (1) 65,536 * Network Packet Size

Length of a string containing SQL statements (batch size) (1) 65,536 * Network packet size

(1) Network 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.

To my mind, that means 268,435,456 bytes when using defaults. Please say your query is shorter than that.

like image 134
Neil Moss Avatar answered Oct 25 '22 17:10

Neil Moss