SQL:错误,已达到表达式服务限制?
“内部错误:已达到表达式服务限制。请在查询中查找可能复杂的表达式,并尝试简化它们。”
有人以前见过这个并找到了一个好的解决方法吗?
我设法解决这个问题,方法是将我的 SQL 查询本质上分成两部分,并将第一个 SQL 选择查询写入临时表,第二部分,一个新的 SQL 选择语句从临时表中进行选择,并使用大量的 CROSS APPLY 运算符计算级联计算列。
这是第二部分的示例,但我使用了更多的交叉应用来生成新的计算列:
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
我的目标是将其放入 sql 报告中,如果只有 1 个用户作为 < code>#tempTable 将在同一次执行中创建和删除,并且结果正确显示在报告中。但将来如果有并发用户,我担心他们会写入同一个#tempTable
,这会影响结果吗?
我已经考虑过将其放入存储过程中,但仍然收到上面的错误消息。
"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.
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. 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?
I've looked at putting this into stored procedures but still get the error message above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
出现此问题的原因是 SQL Server 限制了单个查询表达式中可以包含的标识符和常量的数量。限制为 65,535。对标识符和常量数量的测试是在 SQL Server 展开所有引用的标识符和常量之后执行的。在 SQL Server 2005 及更高版本中,查询在内部进行了规范化和简化。其中包括 *(星号)、计算列等。
为了解决此问题,请重写您的查询。在查询的最大表达式中引用较少的标识符和常量。您必须确保查询的每个表达式中的标识符和常量的数量不超过限制。为此,您可能必须将一个查询分解为多个查询。然后,创建临时中间结果。
This issue occurs because SQL Server limits the number of identifiers and constants that can be contained in a single expression of a query. The limit is 65,535. The test for the number of identifiers and constants is performed after SQL Server expands all referenced identifiers and constants. In SQL Server 2005 and above, queries are internally normalized and simplified. 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.
当我们尝试将数据库兼容级别更改为 150 时,同样的问题也发生在我身上。当它为 140 或更低时,这不是问题。
The same issue happens to me when we tried to change the Database Compatibility Level to 150. It is not an issue when it is 140 or lower.
我刚刚遇到了这个问题,并通过删除表上的 UNIQUE 索引来修复它。由于某种原因,这似乎会触发此错误,尽管它无法弄清楚原因。
顺便说一下,同一个查询确实可以与其他几个索引一起使用。
I just had this problem and fixed it by removing the UNIQUE index on my table. 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
替换几个COALESCE
语句What worked for me was replacing several
COALESCE
statements withISNULL
whenever was possible