Linked Server is a very useful feature that allows you to connect to another database instance from the SQL Server Database Engine. It enables the database instance to read data and execute commands against the remote database using an OLE DB provider.
The remote server can be supported by an OLE DB provider: SQL Server, Oracle, MySQL, etc.
Linked Server is also available in Azure, but there are certain restrictions depending on the source and target database:
In this article, I will show you how to successfully set up a linked server between two Azure SQL VMs in different virtual networks.
I’m using the following environment in Azure:
SQL Virtual Machine |
SQL VM IP Address |
Resource Group |
Virtual Network |
VM Subnet |
Address Space |
VNA-SQL1 |
10.0.0.4 |
VNARG |
VNARG-vnet |
10.0.0.0/24 |
10.0.0.0/16 |
VNA-SQL2 |
10.1.0.4 |
VNARG |
VNARG-vnet1 |
10.1.0.0/24 |
10.1.0.0/16 |
Configuring VNET Peering
Virtual Network Peering allows connections between virtual networks within the same Azure region via Azure backbone infrastructure. In this architecture, I will use it to allow communication between VNA-SQL1 and VNA-SQL2.
To create the Virtual Network Peering, I will use Azure Cloud Shell, the interactive environment activated from the Azure portal through a web browser.
To start a Cloud Shell console, select the Cloud Shell button on the menu bar at the upper right in the Azure portal:
Select PowerShell in the left menu, and you will be granted console access:
The process is straightforward. I will create a Virtual Network Peering for each route in the connection. In this case, one VNet Peering for communication from VNARG-vnet to VNARG-vnet1 and one VNet Peering for communication from VNARG-vnet1 to VNARG-vnet.
Let’s start capturing the current VNet information:
$vnet1 = Get-AzVirtualNetwork -Name VNARG-vnet -ResourceGroupName VNARG
$vnet2 = Get-AzVirtualNetwork -Name VNARG-vnet1 -ResourceGroupName VNARG
Then, I will create the two Virtual Network Peering’s defining the Source (VirtualNetwork) and the Destination (RemoteVirtualNetwork):
Add-AzVirtualNetworkPeering -Name SQL1toSQL2 -VirtualNetwork $vnet1 -RemoteVirtualNetworkId $vnet2.Id
Add-AzVirtualNetworkPeering -Name SQL2toSQL1 -VirtualNetwork $vnet2 -RemoteVirtualNetworkId $vnet1.Id
After configuring both Virtual Network Peering’s, VNA-SQL1 and VNA-SQL2 can communicate with each other. Below is a ping test before and after configuration.
From VNA-SQL1 to VNA-SQL2:
From VNA-SQL2 to VNA-SQL1:
Note: By default, Ping (ICMP) is not allowed by Windows Firewall in an Azure SQL VM; you must enable the firewall rule before testing a ping command.
Configuring Linked Server
Once the communication is set up between both SQL VMs, we can configure the Linked Server.
I will configure VNA-SQL2 as a linked server within VNA-SQL1.
In the first step, I must define a network inbound security rule to allow traffic between VNA-SQL1 and VNA-SQL2.
On the Azure portal, I go to VNA-SQL1 Virtual Machine, Networking, and Add the inbound security rule:
Use the following data:
Next, I can configure a linked server by using SQL Server Management Studio or the sp_addlinkedserver (Transact-SQL) statement to create the linked server. If you want to use SSMS in your DB connection, go to Server Objects, Linked Servers. Right click on Linked Servers and “New Linked Server.” Fill the wizard with the desired options to create the connection to the target DB.
I’m using a script to create the linked server; I will briefly explain the used server options. The same options are available from the New Linked Server wizard. From VNA-SQL1, I execute:
Then, I define the options:
Finally, I’m mapping the local logins as follows:
Local Login |
Impersonate |
Remote Login |
VNA |
True |
VNA |
SQL1\vnaadmin |
False |
VNA |
Notes:
Testing the Linked Server
The linked server will appear in the folder: Server Objects, Linked Servers, Server Name. The available databases in the linked server will appear inside “Catalogs.”
The VNA-SQL2 server has a database called VNADEMO, a locally executed query is shown below:
I will execute the same query from the VNA-SQL1 server to confirm that the linked server is working.
The syntax to execute statements in a linked server is:
select [columns] from [linkedserver].[database_name].[schema_Name].[object_name]
Linked server is working fine between the two VMs. On Azure, Linked servers are available in SQL Server Database Engine and Azure SQL Managed Instance. They are not enabled in Azure SQL Database singleton and elastic pools.
Additional notes: