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

前端 未结 3 471
生来不讨喜
生来不讨喜 2021-01-05 01:48

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 quit

3条回答
  •  臣服心动
    2021-01-05 02:12

    Necromancing, because I now know the real-life answer.

    Technically, like Neil Moss said, > 268 million bytes (ASCII characters), or > 134 million UTF-16 characters.

    However, there is a limit of "256 tables per SELECT statement".

    And while you can use more than 256 tables in a query WITH UNION STATEMENTS, such a query cannot be used as a view, table-valued function or as a subquery for a SELECT statement.

    Also, if you use such a query directly, you may hit the stack space limit of the query optimizer at around 1'300-1'500 SELECTs.

    I know, because thanks to a great (warning - sarcasm) OCP (original code "programmer"), we have such a f*ed up schema, and I actually ran into both, the 256 and the 1'300-1'500 limit.

    (one table per object type, in case anybody wonders; with exactly the same column-schema (but not names) per table - instead of a reference table with object-type...)

提交回复
热议问题