Asked By Erland Sommarskog
13-Jan-08 06:59 AM

Hassan (hassan@hotmail.com) writes:
OPENQUERY does not accept an expression for the query. It has to be a
constant. This is because, SQL Server actually attempts to compile a plan
for the complete query, which is kind of difficult if the remote
query is unknown. (Hey, Kalen, you should have been around long enough
to know this. :-)
This leads to dynamic SQL, and in an orgy of nested quotes which
easily can drive you crazy. Uri's post looks scary, and but that is
just the beginning.
If you don't want to join the query reesult with something else, EXEC AT
is easier to use:
EXEC('SELECT * FROM mysqltable WHERE id > ?', @testid) AT mysqlhost
The ? is a placeholder for a parameter.
If you need to use OPENQUERY, have a look at
http://www.sommarskog.se/dynamic_sql.html#OPENQUERY for some tips for
mitigating the quote craze.
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx