The query processor ran out of stack space during query optimization. Please simplify your query

Amit Kadam 0 Reputation points
2023-02-22T15:00:58.6966667+00:00

code.txt

Hi All,

I have received below error during executing attached query.

The query processor ran out of stack space during query optimization. Please simplify the query.

It properly worked for @lineCount = 500 but yesterday we received @lineCount = 1200 first time

and it gives the error.

Please help me resolve this issue

Thanks

SQL Server | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Guoxiong 8,206 Reputation points
    2023-02-22T23:16:14.7666667+00:00

    Did you try this:

    DECLARE @strQuery VARCHAR(MAX) = ''
    SET @strQuery = @strQuery0 + @strQuery1 + @strQuery2 + @strQuery3 + @strQuery4;
    EXECUTE(@strQuery);
    

  2. LiHongMSFT-4306 31,571 Reputation points
    2023-02-23T02:10:54.13+00:00

    Hi @Amit Kadam

    Simplify the query by breaking the query into multiple queries along the largest dimension.

    First remove any query elements that are not really necessary, then try adding a temp table and splitting the query in two.

    Merely moving a part of the query to a subquery or function or common table expression is insufficient, since they get recombined by the Transact-SQL compiler.

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.