简体   繁体   English

SQL 服务器 - 内部错误:已达到表达式服务限制

[英]SQL Server - Internal error: An expression services limit has been reached

I have a SQL Server stored procedure with nearly 300 variables within it.我有一个 SQL 服务器存储过程,其中包含近 300 个变量。

The variables were created as follows:变量创建如下:

CASE WHEN Grp1 = 'SALES' AND grp_mnth = MONTH_0 THEN SUM(Col)  OVER(PARTITION BY grp_loc,COMM) ELSE 0 END AS "SALES_1"

As this has data issue all the variables were replaced like:由于这有数据问题,所有变量都被替换为:

SUM(CASE WHEN Grp1 = 'SALES' AND grp_mnth = MONTH_0 THEN Col ELSE 0 END)  OVER(PARTITION BY grp_loc,COMM) AS "SALES_1"

The data issue has been solved by the above statement, but the procedure throws the below error when we add all the 300 variables, if only about 100 is added, the procedure runs fine.上面的语句已经解决了数据问题,但是当我们添加所有300个变量时,程序会抛出以下错误,如果只添加大约100个,程序运行正常。

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

As per the answer posted I have tried to split the query to multiple select queries and the error has been resolved, but while combining the result the data has not been fetched correctly.根据发布的答案,我尝试将查询拆分为多个 select 查询,错误已解决,但在组合结果时,数据未正确获取。

BEGIN

CREATE TABLE #TEMPTAB1
(
TYPE_1 char(15),
NUMBER_1 char(7),
STATUS_1 char(1),
...
)
CREATE TABLE #TEMPTAB2
(
TYPE_2 char(15),
NUMBER_2 char(7),
STATUS_2 char(1),
...
)
CREATE TABLE #TEMPTAB3
(
TYPE_3 char(15),
NUMBER_3 char(7),
STATUS_3 char(1),
...
)

SELECT * FROM 
    #TEMPTAB1 T1 
     INNER JOIN 
    #TEMPTAB2 T2 ON T1.TYPE_1=T2.TYPE_2 AND T1.NUMBER_1 = T2.NUMBER_2 AND T1.STATUS_1 = T2.STATUS_2
     INNER JOIN 
    #TEMPTAB3 T3 ON T1.TYPE_1=T3.TYPE_3 AND T1.NUMBER_1 = T3.NUMBER_3 AND T1.STATUS_1 = T3.STATUS_3

END

Can anyone please suggest a way to correct the joins in the above code.任何人都可以建议一种方法来更正上述代码中的连接。

Please re-write your query!请重新编写您的查询!

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 限制了可以包含在查询的单个表达式中的标识符和常量的数量。 This limit is 65,535.此限制为 65,535。

one approach might be :一种方法可能是:

You can split the select query to multiple select queries.Store the result in temp tables and combine the results at the end.您可以将选择查询拆分为多个选择查询。将结果存储在临时表中并在最后合并结果。

more info更多信息

Fo me it was the SQL Server version.对我来说,它是 SQL Server 版本。 I had no issue with running the same query against SQL Server 2017 on production but was facing the issue with SQL Server 2018 on staging.我在生产中对 SQL Server 2017 运行相同的查询没有问题,但在登台时遇到了 SQL Server 2018 的问题。 I downgraded my SQL Server version back to 2017 from 2018 on production and the issue was resolved.我在生产中将我的 SQL Server 版本从 2018 年降级到 2017 年,问题得到解决。 This might be an issue with SQL Server 2018 as of now到目前为止,这可能是 SQL Server 2018 的问题

Right click on your Database, inside database properties, click on options, then change compatibility level to lower one in the list.右键单击您的数据库,在数据库属性中,单击选项,然后将兼容性级别更改为列表中的较低级别。 See this image compatibility level查看此图像兼容性级别

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

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