How to work around SQL Server's “The maximum number of tables in a query (260) was exceeded.”

跟風遠走 提交于 2019-12-02 01:51:39

A colleague came up with a great answer. Use a function to return a table variable; insert the results into the table variable bit by bit:

CREATE VIEW dbo.USGovCurrencyOnHandBreakdown AS
    SELECT * FROM fn_USGovCurrencyOnHandBreakdown()

with the view now calling the 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

As far as any clients know the view is unchanged. (Except now it works!)

You could store the subqueries into temp tables, e.g. the USGovCurrencyOnHandBreakdown_Additions and USGovCurrencyOnHandBreakdown_Subtractions that you mentioned, and than selecting from those temp tables instead of views.

Of course, the transactions could be an issue because of the dirty reads, I don't know if that's a concern in this case...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!