Saturday, December 15, 2012

Internal Query Processor Error: The query processor could not produce a query plan

OK folks, it did happen a few days ago to get this error message:

Internal Query Processor Error: The query processor could not produce a query plan. For more information, contact Customer Support Services.

I was trying to execute the following query:

SELECT * 
FROM dbo.tbl1
WHERE 
tbl2ID=(SELECT id FROM dbo.tbl2 WHERE Code='Code1')
AND 
tbl3ID=(SELECT id FROM dbo.tbl3 WHERE Code='Code2')

As you can see, in the above query I'm using two subqueries for getting some keys for use in my main query. In some cases you might get the aforementioned error message. However, this is not always the case as it depends on many factors and not only on the query itself.

Anyway, in order to resolve the issue I just had to re-write the query. The re-written query is the following:

SELECT  t1.* 
FROM dbo.tbl1 t1, dbo.tbl2 t2, dbo.tbl3 t3
WHERE 
t1.tbl2ID=t2.ID 
AND t1.tbl3ID=t3.ID
AND t2.Code='Code1'
AND t3.code='Code2' 

As you can see, I just removed the subqueries and replaced them with additional joins on my main query. Now my query works like a charm!

*** Before changing your query or doing anything else, first, make sure that your instance of SQL Server has the latest service pack installed.
[Ads]
Check out my latest eBook on SQL Server:
Tuning SQL Server - Ebook

Easily secure your SQL Server instances! Try out DBA Security Advisor!
DBA Security Advisor for SQL Server by SQLArtBits
DBA Security Advisor was developed by SQLArtBits. The tool was being carefully developed for an entire year and it contains comprehensive security checks and recommendations based on proven SQL Server security best practices. Feel free to download and use the Community Edition which is free and if you find it useful, you can consider upgrading to the Enterprise Edition!

Artemakis Artemiou [MVP]

Author & Editor

Artemakis Artemiou is a Senior SQL Server Architect, Software Developer and Microsoft Data Platform MVP. He is also an author, regular blogger, president of Cyprus.NET User Group and CY Country leader of INETA-EU. He is also the creator of DBA Security Advisor and In-Memory OLTP Simulator. Artemakis is a frequent guest author of worldwide well-respected online journals where he writes articles focusing on many SQL Server topics.

Reference: The SQL Server and .NET Hub (http://www.sqlnethub.com)

0 comments: