How to query remote data sources in MsSQL using Linked Server?

In Microsoft SQL Server, a Linked Server allows you to connect and query remote data from within SQL Server Management Studio (SSMS). You can access data from databases like Oracle, MySQL, and PostgreSQL or even non-database sources such as Excel or flat files.

 

How to Query Remote Data Sources in MSSQL Server Using Linked Servers

 

Step 1: Create a Linked Server

Before querying remote data, you must set up a Linked Server connection between your local SQL Server and the remote data source.

Using SSMS:

Step 1: Launch SQL Server Management Studio and connect to your SQL Server instance.

Step 2: In Object Explorer, go to Server Objects -> Linked Servers, right-click, and select New Linked Server.

Step 3: Fill in the Details

Linked Server: Provide a name for the Linked Server.

Server Type: Choose "SQL Server" for a SQL Server remote source, or "Other data source" for non-SQL sources.

Data Source: Enter the remote server’s name or IP address.

Provider String: For non-SQL sources (optional).

Catalog: Default database name (optional).

 

Step 4: Choose how to pass login credentials (e.g., Be made using this security context with remote login details).

Step 5: After entering the details, click OK and test the connection.

 

Using T-SQL:

You can also create a Linked Server with SQL queries:

EXEC sp_addlinkedserver 

   @server = 'LinkedServerName', 

   @srvproduct = 'SQL Server', 

   @provider = 'SQLNCLI', 

   @datasrc = 'RemoteServerName';

For non-SQL sources:

EXEC sp_addlinkedserver 

   @server = 'LinkedServerName',

   @provider = 'ProviderName', 

   @datasrc = 'RemoteDataSource';

 

Step 2: Set Up Security Settings

You need to decide how SQL Server will authenticate with the remote server. This means linking your local SQL Server logins to the remote server logins.

 

Using SSMS:

Step 1: Go to the Security page in the Linked Server properties.

Step 2: Choose how you want to map local logins to the remote server logins.

Step 3: You can also set a default login by selecting Be made using this security context and entering a remote username and password.

 

Using T-SQL:

EXEC sp_addlinkedsrvlogin 

   @rmtsrvname = 'LinkedServerName', 

   @useself = 'false', 

   @locallogin = NULL,              -- Applies to all local logins or a specific one

   @rmtuser = 'RemoteUser',         -- Remote server username

   @rmtpassword = 'RemotePassword'; -- Remote server password

 

Step 3: Query the Linked Server

After setting up the Linked Server, you can query the remote data using a four-part name format.

Syntax:

SELECT * 

FROM [LinkedServerName].[DatabaseName].[Schema].[TableName];

Example:

If the remote server is called RemoteSQL, the database is SalesDB, and the table is Orders, the query would look like this:

SELECT * 

FROM [RemoteSQL].[SalesDB].[dbo].[Orders];

 

Step 4: Join Data from Local and Remote Servers

You can combine data from both local and remote servers in a single query.

Example:

This query joins a local table with a table on the remote server:

SELECT localTable.Name, remoteTable.OrderDate

FROM LocalDB.dbo.Customers AS localTable

JOIN [RemoteSQL].[SalesDB].[dbo].[Orders] AS remoteTable

   ON localTable.CustomerID = remoteTable.CustomerID;

 

Step 5: Test the Linked Server Connection

To check if the Linked Server is set up correctly, run the following command:

EXEC sp_testlinkedserver 'LinkedServerName';

 

Step 6: Optimize Queries (Optional)

If remote queries are slow, you can use OPENQUERY or pass-through queries to improve performance.

Example of using OPENQUERY:

SELECT * 

FROM OPENQUERY(LinkedServerName, 'SELECT * FROM SalesDB.dbo.Orders');

Important Note:

Four-part name: [LinkedServer].[Database].[Schema].[Table].

Security: Ensure login mappings are set up correctly to avoid authentication issues.

Performance: Linked Servers can be slow for large datasets, so consider optimizing queries or using tools like SSIS for bulk data movement.

 

Conclusion:

Querying remote data in MS SQL Server using Linked Server involves setting up a connection, configuring security, and using a four-part naming format. You can easily run queries, join data from local and remote servers, and optimize performance with OPENQUERY. While Linked Servers make accessing remote data simple, proper security and performance tuning are important for efficient use.


Was this answer helpful?

« Back

chat