Open In App

Understanding the OPENQUERY Function in SQL Server

Last Updated : 26 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The OPENQUERY function in SQL Server is a powerful tool that enables users to execute pass-through queries on linked servers. It allows SQL statements to be run on remote database servers OPENQUERY and facilitates the retrieval and manipulation of data from different data sources that cannot be accessed directly through standard T-SQL commands.

In this article, we will explain the OPENQUERY function, and its parameters and provide examples of how it is used in SQL Server.

OPENQUERY Function in SQL Server

  • The OPENQUERY function in SQL Server is used to execute a pass-through query on a linked server.
  • It allows us to run a SQL statement on a remote database server and return the result set to the local server.
  • This is particularly useful for querying different data sources that are not directly accessible using standard T-SQL commands.

Syntax:

OPENQUERY ( linked_server , 'query' )

Explanation:

  • linked_server: The name of the server that is linked with our server and where the query is going to be performed.
  • query: The statement is sent for execution to the remote server in SQL. It can only be in single quotes (‘)

Environment Setup

Before using OPENQUERY, we must first configure a linked server. This can be done through SQL Server Management Studio (SSMS) or by using T-SQL commands.

Query:

EXEC sp_addlinkedserver

@server='LinkedServer_A',

@srvproduct='',

@provider='SQLNCLI',

@datasrc='RemoteServerName';

Examples of OPENQUERY Function in SQL Server

Example 1: Run Select Statement

Suppose we have a linked server named LinkedServer_A that points to another SQL Server instance. we want to retrieve data from the Employees table on the remote server.

Query:

SELECT * FROM OPENQUERY(LinkedServer_A, 'SELECT EmployeeID, FirstName, LastName FROM Employees')

Output:

LinkedServer_A
LinkedServer_A

Explanation:

This query will perform SELECT EmployeeID, FirstName, LastName FROM Employees statement in LinkedServer_A. It is then passed back to the local SQL Server.

Example 2: Insert Data in the Local Table

In this query, OPENQUERY is used to fetch employee data from the remote server where the DepartmentID is 1. The retrieved data is then inserted into the local table . This allows transferring specific data from the remote server to the local database

Query:

INSERT INTO LocalTable (EmployeeID, FirstName, LastName)

SELECT *

FROM OPENQUERY(LinkedServer_A, 'SELECT EmployeeID, FirstName, LastName FROM Employees WHERE DepartmentID = 1');

Output:

Local-Table
Local Table

Explanation:

Here, we use OPENQUERY and join the result of the query with the local Departments table in order to get the department name of each employee.

Example 3: Execute the DML Statements

In this query, OPENQUERY is used to insert a new employee, "Jessica Smith," with an EmployeeID of 4, into the Employees table on the remote server (LinkedServer_A). The data is inserted directly into the remote table.

Query:

INSERT INTO OPENQUERY(LinkedServer_A, 'SELECT * FROM Employees')

VALUES (4, 'Jessica', 'Smith');

Output:

Employee-table
Employee Table with DML statement

Explanation:

After executing the query, the Employees table on the remote server (LinkedServer_A) will have a new record added. The new row will contain the values: EmployeeID = 4, FirstName = 'Jessica', and LastName = 'Smith'. This updates the table with the inserted employee data.

Conclusion

In summary, OPENQUERY serves as a vital function for SQL Server users who need to interact with linked servers efficiently. By enabling the execution of complex queries and data manipulation directly on remote servers, it enhances data integration capabilities and optimizes performance.


Next Article
Article Tags :

Similar Reads