Lesson Learned #176: Using Polybase to connect from SQL OnPremises to Azure Managed Instance/SQLDB

Published Jun 15 2021 02:23 PM 869 Views

Today, I got a very interesting question about if could be possible to connect from external tables to Azure SQL Managed Instance, SQL Database or Synase. In this article, I would like to explain it. 

 

Besides the option that we have with Linked Server, my first option was to use SQL SERVER 2019 and Polybase, after installing Polybase and using the following TSQL statement I was able to connect to Managed Instance, SQL Database and Synapse from my OnPremises or Azure Virtual Machine. 

 

 

CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Password';
CREATE DATABASE SCOPED CREDENTIAL AzureSQLExternalTableCredentials  WITH IDENTITY = 'UserName', Secret = 'Password';
CREATE EXTERNAL DATA SOURCE AzureSQLExternalTableDataSource WITH (LOCATION = 'sqlserver://servername.database.windows.net', PUSHDOWN = ON, CREDENTIAL = AzureSQLExternalTableCredentials);
CREATE EXTERNAL TABLE [dbo].[AzureSQLExternalTable_MyTable] ([id] [int] NOT NULL)         WITH (DATA_SOURCE = AzureSQLExternalTableDataSource ,location='databasename.schemaname.TableName')
    

 

 

Running a query Select * from AzureSQLExternaTable_MyTable I was able to obtain the data. 

 

Unfortunately, it is not possible to insert data to the table AzureSQLExternalTable_MyTable because external tables in AzureSQL, Synapse and SQL Server OnPrem there is not supported run DML commands.

 

Enjoy!

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2451043%22%20slang%3D%22en-US%22%3ELesson%20Learned%20%23176%3A%20Using%20Polybase%20to%20connect%20from%20SQL%20OnPremises%20to%20Azure%20Managed%20Instance%2FSQLDB%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2451043%22%20slang%3D%22en-US%22%3E%3CP%3EToday%2C%20I%20got%20a%20very%20interesting%20question%20about%20if%20could%20be%20possible%20to%20connect%20from%20external%20tables%20to%20Azure%20SQL%20Managed%20Instance%2C%20SQL%20Database%20or%20Synase.%26nbsp%3BIn%20this%20article%2C%20I%20would%20like%20to%20explain%20it.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBesides%20the%20option%20that%20we%20have%20with%20Linked%20Server%2C%20my%20first%20option%20was%20to%20use%20SQL%20SERVER%202019%20and%20Polybase%2C%20after%20installing%20Polybase%20and%20using%20the%20following%20TSQL%20statement%20I%20was%20able%20to%20connect%20to%20Managed%20Instance%2C%20SQL%20Database%20and%20Synapse%20from%20my%20OnPremises%20or%20Azure%20Virtual%20Machine.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20MASTER%20KEY%20ENCRYPTION%20BY%20PASSWORD%20%3D%20'Password'%3B%0ACREATE%20DATABASE%20SCOPED%20CREDENTIAL%20AzureSQLExternalTableCredentials%20%20WITH%20IDENTITY%20%3D%20'UserName'%2C%20Secret%20%3D%20'Password'%3B%0ACREATE%20EXTERNAL%20DATA%20SOURCE%20AzureSQLExternalTableDataSource%20WITH%20(LOCATION%20%3D%20'sqlserver%3A%2F%2Fservername.database.windows.net'%2C%20PUSHDOWN%20%3D%20ON%2C%20CREDENTIAL%20%3D%20AzureSQLExternalTableCredentials)%3B%0ACREATE%20EXTERNAL%20TABLE%20%5Bdbo%5D.%5BAzureSQLExternalTable_MyTable%5D%20(%5Bid%5D%20%5Bint%5D%20NOT%20NULL)%20%20%20%20%20%20%20%20%20WITH%20(DATA_SOURCE%20%3D%20AzureSQLExternalTableDataSource%20%2Clocation%3D'databasename.schemaname.TableName')%0A%20%20%20%20%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERunning%20a%20query%20%3CSTRONG%3ESelect%20*%20from%20AzureSQLExternaTable_MyTable%3C%2FSTRONG%3E%20I%20was%20able%20to%20obtain%20the%20data.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUnfortunately%2C%20it%20is%20not%20possible%20to%20insert%20data%20to%20the%20table%20AzureSQLExternalTable_MyTable%20because%20external%20tables%20in%20AzureSQL%2C%20Synapse%20and%20SQL%20Server%20OnPrem%20there%20is%20not%20supported%20run%20DML%20commands.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEnjoy!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2451043%22%20slang%3D%22en-US%22%3E%3CP%3EToday%2C%20I%20got%20a%20very%20interesting%20question%20about%20if%20could%20be%20possible%20to%20connect%20from%20external%20tables%20to%20Azure%20SQL%20Managed%20Instance%2C%20SQL%20Database%20or%20Synase.%26nbsp%3BIn%20this%20article%2C%20I%20would%20like%20to%20explain%20it.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2451043%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzureSQL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Jun 15 2021 02:24 PM
Updated by: