sql-serverazure-sql-databaseexternal-tables

How create external table in Azure SQL to access a table in local SQL Server on same subnet. Getting error on select


I have a number of Azure SQL databases that I would like to have access an external table in a local SQL Server database (not named instance). The local SQL Server table has a trigger that can send mail that I want to use since Azure SQL databases don't have mail capabilities that could send an email. Stored procedures in the Azure SQL databases would write to the local database and trigger the event.

I have done the following:

CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'xxxx';
CREATE DATABASE SCOPED CREDENTIAL [SignalRxCredential] 
    WITH IDENTITY='normalLogon',SECRET='normalPassword';  -- SQL Server authentication

CREATE EXTERNAL DATA SOURCE [SignalRxDataSource]    
WITH (
    TYPE = RDBMS,  
    LOCATION = 'sqlserver://10.0.0.6',  
    DATABASE_NAME = 'staging',  
    CREDENTIAL = [SignalRxCredential]
);

CREATE EXTERNAL TABLE [dbo].[EmailNotifications_ext]
(
    [MessageID] [int] NOT NULL,
    [Subject] [varchar](255) NULL,
    [Recipient] [varchar](255) NULL,
    [SendFrom] [varchar](255) NULL,
    [MessageText] [nvarchar](max) NULL
) 
WITH (DATA_SOURCE=[SignalRxDataSource],SCHEMA_NAME=N'dbo',OBJECT_NAME=N'EmailNotifications')

It successfully creates the scoped credential and the external table but when I try to select records from it, I get this error:

An error occurred while establishing connection to remote data source: [Microsoft][ODBC Driver 17 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53].
[Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired[Microsoft][ODBC Driver 17 for SQL Server]
A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.

I've double-checked the credentials and I'm connected to both the Azure SQL server and the local SQL Server is the same SSMS... so both databases are accessible via TCP/IP with SQL Server authentication. Named pipes is enabled and allow remote connections is checked on the local server.

What have I missed and what do I need to do to get this working?


Solution

  • Azure SQL Database does not support direct connections to SQL Server instances for external tables. This is why you're encountering connection errors.

    If you are trying to create that external table on the basis that Azure SQL does not support table triggers, please note that Azure SQL do support triggers as stated on the documentation.

    Below you will find how to create a trigger on Azure SQL:

    create table testusers  
    (userid int,  
    [password] varchar(20))  
      
    insert into testusers values  
    (1,'password1'),  
    (2,'password2'),  
    (3,'password3')  
      
    create table pswlog  
    (userid int,  
    oldpassword varchar(20),  
    newpassword varchar(20),  
    updatetime datetime)  
    
    CREATE TRIGGER psw_trigger  
    ON testusers  
    AFTER  UPDATE  
    AS  
    IF ( UPDATE ([password]) )  
    BEGIN  
    insert into pswlog   
    select a.userid,c.password,a.password,getdate() from inserted a  
    inner join testusers b on a.userid=b.userid  
    inner join deleted c on a.userid=c.userid  
    END;  
    
    -- Fire the trigger
    
    update  testusers set password='password11' where userid=1  
    update  testusers set password='password111' where userid=1  
    update  testusers set password='password22' where userid=2 
    
    -- Examine log created by the trigger
    
    select * from pswlog