In many enterprise environments, data is distributed across multiple SQL Servers or even different database systems (like Oracle, MySQL, or PostgreSQL). Instead of manually exporting/importing data, SQL Server offers a powerful feature called Linked Server that lets you query and manipulate data across multiple servers β just like they were part of the same database.
This article explains what a Linked Server is, how to configure it step-by-step, and how to query remote data using practical examples.
π What is a Linked Server?
A Linked Server in SQL Server allows you to connect to another database server instance (on the same network or remote) and execute distributed queries (SELECT, INSERT, UPDATE, DELETE) against OLE DB data sources outside of the local SQL Server.
It enables:
βοΈ Setting Up a Linked Server
You can create a Linked Server via SQL Server Management Studio (SSMS) or T-SQL script.
β
Method 1: Using SSMS GUI
Open SSMS β Expand Server Objects β Right-click on Linked Servers β Choose New Linked Server
In the dialog box:
Linked server: Enter an alias name (e.g., LinkedServer_Prod
)
Server type: Choose SQL Server or Other data source
Provider: Select Microsoft OLE DB Provider for SQL Server
Data source: Enter remote server name or IP
Go to the Security tab and configure credentials:
Click OK to create the Linked Server.
β
Method 2: Using T-SQL Script
Hereβs how to create a Linked Server using SQL script:
EXEC sp_addlinkedserver
@server = 'LinkedServer_Prod',
@srvproduct = '',
@provider = 'SQLNCLI',
@datasrc = '192.168.1.100'; -- Remote Server IP or Name
EXEC sp_addlinkedsrvlogin
@rmtsrvname = 'LinkedServer_Prod',
@useself = 'false',
@locallogin = NULL,
@rmtuser = 'sa',
@rmtpassword = 'StrongPassword123';
π§ͺ Example: Querying Data from a Linked Server
Once the Linked Server is created, you can query it using four-part naming convention:
[LinkedServerName].[DatabaseName].[SchemaName].[TableName]
Example 1: Simple SELECT query
SELECT TOP 10 *
FROM LinkedServer_Prod.SalesDB.dbo.Customers;
Example 2: Joining Local and Remote Tables
SELECT
a.OrderID,
a.CustomerID,
b.CustomerName
FROM LocalDB.dbo.Orders a
INNER JOIN LinkedServer_Prod.SalesDB.dbo.Customers b
ON a.CustomerID = b.CustomerID;
Example 3: Executing Remote Stored Procedure
EXEC LinkedServer_Prod.SalesDB.dbo.sp_GetTopCustomers @TopCount = 5;
π§° Updating Remote Data
You can even insert or update remote tables via Linked Server.
Example 4: Insert into remote table
INSERT INTO LinkedServer_Prod.SalesDB.dbo.Customers (CustomerName, City)
VALUES ('Vipin Mittal', 'Jaipur');
Example 5: Update remote data
UPDATE LinkedServer_Prod.SalesDB.dbo.Customers
SET City = 'Udaipur'
WHERE CustomerID = 101;
π§ Best Practices
β
Use SQL authentication with strong passwords for remote login.
β‘ Enable RPC and RPC Out only if needed.
π§© Use OPENQUERY() for performance optimization with complex joins.
π Limit access by creating specific database roles and permissions.
π Using OPENQUERY (Performance Tip)
Instead of the four-part naming method, use OPENQUERY
to push the query execution to the remote server:
SELECT *
FROM OPENQUERY(LinkedServer_Prod, 'SELECT CustomerName, City FROM SalesDB.dbo.Customers WHERE City = ''Jaipur''');
This approach reduces data transfer and often performs faster.
π§Ή Removing a Linked Server
When you no longer need a Linked Server, remove it safely:
EXEC sp_dropserver 'LinkedServer_Prod', 'droplogins';
π References (External Links)
Here are some official and community resources for further reading:
π Microsoft Docs β sp_addlinkedserver (Transact-SQL)
π Microsoft Docs β Linked Servers Overview
π SQLShack β Querying remote data sources in SQL Server
π Redgate β Managing Linked Servers securely
π Conclusion
Linked Servers in SQL Server are a powerful way to integrate and access distributed data sources without complex ETL processes. With proper configuration, they can significantly improve data collaboration and reduce maintenance efforts across multiple systems.
However, always monitor performance and secure connections to prevent unauthorized access.