While running a linked server query, I encountered an error and learned something new. In this blog we would learn about how to fix error – Msg 8180 – Statement(s) could not be prepared.
Here is the complete error message which I received.
OLE DB provider “SQLNCLI11” for linked server “SQL2019” returned message “Deferred prepare could not be completed.”.
Msg 8180, Level 16, State 1, Line 13
Statement(s) could not be prepared.
Msg 4104, Level 16, State 1, Line 13
The multi-part identifier “NAME.ID” could not be bound.
Reproducing the error is very easy. In my case, I created a linked server (called as SQL2019) and ran below query.
SELECT * FROM OPENQUERY([SQL2019],'SELECT NAME . ID FROM SYS.DATABASES')
WORKAROUND/SOLUTION
When I captured profiler, I was able to understand the meaning of it. We can see below in profiler.
The statement which came to the linked server was
declare @p1 int set @p1=0 exec sp_prepare @p1 output,NULL,N'SELECT NAME . ID FROM SYS.DATABASES',1 select @p1
and that failed as seen in profiler.
The message essentially means that the statement could not be compiled on the destination server. Based on my search on the internet, we should see the real error at the end of multiple messages. In this situation the error is
Msg 4104, Level 16, State 1, Line 13
The multi-part identifier “NAME.ID” could not be bound.
Of course, I know the error. I have put dot instead of a comma to generate an error.
Reference: Pinal Dave (https://blog.sqlauthority.com)
First appeared on SQL SERVER – FIX: Msg 8180 – Statement(s) Could not be Prepared. Deferred Prepare Could not be Completed