In SQL Server, linked servers allow you to connect to and run queries against external data sources (like another SQL Server, Oracle, MySQL, or even Excel) as if they were part of your local server. When you use open queries with linked servers, you’re using a specific method to send SQL statements directly to the remote server for execution.
Why Use OPENQUERY with Linked Servers?
OPENQUERY is a pass-through query: it sends the SQL command as-is to the remote server. Here’s why that’s useful:
Advantages of Using OPENQUERY
1. Performance Boost
- The query is executed on the remote server, and only the result set is returned. This reduces network traffic and the remote server does the work.
2. Bypass SQL Server Parsing
- Since the query is passed directly, SQL Server doesn’t parse or optimise it, this is useful when the remote server uses non-T-SQL syntax (e.g., Oracle SQL).
3. Access Remote Features
- You can use remote-specific functions, procedures, or syntax that wouldn’t work in a four-part name query.
4. Avoid Compatibility Issues
- Helps when the remote server has different collation, data types, or reserved keywords.
When Not to Use OPENQUERY
- If you need to dynamically build queries, OPENQUERY is less flexible (you’d need dynamic SQL).
- If you want to join local and remote tables, four-part naming might be more straightforward.
From my SQL Server DBA handbook.