简体   繁体   English

SQL:错误,已达到表达式服务限制?

[英]SQL: Error, Expression services limit reached?

"Internal error: An expression services limit has been reached. Please look for potentially complex expressions in your query, and try to simplify them." “内部错误:已达到表达式服务限制。请在您的查询中查找潜在的复杂表达式,并尝试简化它们。”

Has anyone seen this before and found a good workaround?有没有人以前见过这个并找到了一个很好的解决方法?

I managed to get around this issue by splitting my SQL query into two parts essentially and writing the first SQL select query to a temp table and the second part, a new SQL select statement selects from the temporary table and uses alot of CROSS APPLY operator to Calculate cascading computed columns.我设法通过将我的 SQL 查询本质上分为两部分并将第一个 SQL 选择查询写入临时表和第二部分,一个新的 SQL 选择语句从临时表中选择并使用大量 CROSS APPLY 运算符来解决这个问题计算级联计算列。

This is an example of how the second part looks but I'm using alot more Cross Applys to produce new columns which are calculations:这是第二部分外观的示例,但我使用了更多交叉应用来生成新的计算列:

Select * from #tempTable        

cross apply
    (
      select HmmLowestSalePrice =
       round(((OurSellingPrice + 1.5) / 0.95) - (CompetitorsLowestSalePrice) + 0.08, 2)
    ) as HmmLowestSalePrice 

cross apply
    (
      select checkLowestSP =
       case 
        when adjust = 'No Room' then 'No Room'
        when OrginalTestSalePrice >= CompetitorsLowestSalePrice then 'Minus'
        when OrginalTeslSalePrice < CompetitorsLowestSalePrice then 'Ok'
      end
) as checkLowestSP  

cross apply
    (
        select AdjustFinalNewTestSP =
        case
        when FinalNewTestShipping < 0 Then  NewTestSalePrice - (FinalNewTestShipping)
        when FinalNewTestShipping >= 0 Then NewTestSalePrice
        end
) as AdjustFinalNewTestSP

cross apply
    (
      select CheckFinalSalePriceWithWP  =
      case 
        when round(NewAdminSalePrice, 2) >= round(wholePrice, 2) then 'Ok'

        when round(NewAdminSalePrice, 2) < round(wholePrice, 2) then 'Check'
      end
    ) as CheckFinalPriceWithWP 


DROP TABLE #tempTable

My goal to to put this into a sql report and it work fine if there is 1 user only as the #tempTable will get created and dropped in the same execution and the results are displayed in the report correctly.我的目标是将其放入 sql 报告中,如果只有 1 个用户,它可以正常工作,因为#tempTable将在同一执行中创建和删除,并且结果正确显示在报告中。 But in the future if there are concurrent users I'm concerned that they will be writing to the same #tempTable which will affect the results?但是将来如果有并发用户,我担心他们会写入同一个#tempTable这会影响结果吗?

I've looked at putting this into stored procedures but still get the error message above.我已经考虑将其放入存储过程,但仍然收到上面的错误消息。

This issue occurs because SQL Server limits the number of identifiers and constants that can be contained in a single expression of a query.出现此问题的原因是 SQL Server 限制了可以包含在查询的单个表达式中的标识符和常量的数量。 The limit is 65,535.限制为 65,535。 The test for the number of identifiers and constants is performed after SQL Server expands all referenced identifiers and constants.在 SQL Server 展开所有引用的标识符和常量之后,执行标识符和常量数量的测试。 In SQL Server 2005 and above, queries are internally normalized and simplified.在 SQL Server 2005 及更高版本中,查询在内部进行了规范化和简化。 And that includes *(asterisk), computed columns etc.这包括*(星号)、计算列等。

In order to work around this issue, rewrite your query.为了解决此问题,请重写您的查询。 Reference fewer identifiers and constants in the largest expression in the query.在查询的最大表达式中引用较少的标识符和常量。 You must make sure that the number of identifiers and constants in each expression of the query does not exceed the limit.您必须确保查询的每个表达式中的标识符和常量的数量不超过限制。 To do this, you may have to break down a query into more than one single query.为此,您可能必须将查询分解为多个查询。 Then, create a temporary intermediate result.然后,创建一个临时的中间结果。

I just had this problem and fixed it by removing the UNIQUE index on my table.我刚刚遇到了这个问题,并通过删除我表上的 UNIQUE 索引来解决它。 For some reason, that seems to trigger this error, although it cannot figure out why.出于某种原因,这似乎触发了此错误,尽管它无法弄清楚原因。

By the way, the same query does work with several other indexes.顺便说一下,同一个查询确实适用于其他几个索引。

对我ISNULL是尽可能用ISNULL替换几个COALESCE语句

当我们尝试将数据库兼容级别更改为 150 时,同样的问题发生在我身上。当它是 140 或更低时,这不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM