我的查询包含一系列21 UNIONs
,例如:
CREATE VIEW dbo.USGovCurrencyOnHandBreakdown AS
SELECT ... FROM a
UNION ALL
SELECT ... FROM b
UNION ALL
SELECT ... FROM c
UNION ALL
SELECT ... FROM d
...
UNION ALL
SELECT ... FROM u
单独运行时查询运行正常。但是当查询通过包含视图运行时:
SELECT * FROM USGovCurrencyOnHandBreakdown
Msg 4414, Level 16, State 1, Line 1
Could not allocate ancillary table for view or function resolution. The maximum number of tables in a query (260) was exceeded.
我尝试将USGovFedExpentiures
视图拆分为较小的块:
CREATE VIEW dbo.USGovCurrencyOnHandBreakdown AS
SELECT x FROM TreasuryAuditResults
UNION ALL
SELECT x FROM USGovCurrencyOnHandBreakdown_Additions
UNION ALL
SELECT x FROM USGovCurrencyOnHandBreakdown_Subtractions
USGovCurrencyOnHandBreakdown_Additions
和USGovCurrencyOnHandBreakdown_Subtractions
分别包含大约一半的查询:
CREATE VIEW USGovCurrencyOnHandBreakdown_Additions AS
SELECT ... FROM b
UNION ALL
SELECT ... FROM c
...
SELECT ... FROM k
CREATE VIEW USGovCurrencyOnHandBreakdown_Subtractions AS
SELECT ... FROM l
UNION ALL
SELECT ... FROM m
...
SELECT ... FROM u
但是从“父”视图中选择仍然失败:
SELECT * FROM USGovCurrencyOnHandBreakdown
Msg 4414, Level 16, State 1, Line 1
Could not allocate ancillary table for view or function resolution. The maximum number of tables in a query (260) was exceeded.
我如何解决256个表限制?
答案 0 :(得分:3)
一位同事想出了一个很好的答案。使用函数返回表变量;将结果逐位插入表变量:
CREATE VIEW dbo.USGovCurrencyOnHandBreakdown AS
SELECT * FROM fn_USGovCurrencyOnHandBreakdown()
现在调用UDF视图:
CREATE FUNCTION dbo.fn_USGovCurrencyOnHandBreakdown()
RETURNS @Results TABLE
(
Total money,
...
)
INSERT INTO @Results SELECT ... FROM a
INSERT INTO @Results SELECT ... FROM b
INSERT INTO @Results SELECT ... FROM c
INSERT INTO @Results SELECT ... FROM d
...
INSERT INTO @Results SELECT ... FROM u
RETURN
END
任何客户都知道view
没有变化。 (现在它除外了!)
答案 1 :(得分:0)
您可以将子查询存储到临时表中,例如您提到的USGovCurrencyOnHandBreakdown_Additions
和USGovCurrencyOnHandBreakdown_Subtractions
,而不是从那些临时表而不是视图中进行选择。
当然,交易可能是一个问题,因为脏读,我不知道在这种情况下这是否是一个问题......