Remotely run DDL and DML in Synapse from a SQL Managed Instance using Linked Servers

Published Mar 01 2021 11:18 AM 1,734 Views
Microsoft

Imagine you have a SQL Managed Instance and a SQL Dedicated Pool. Now imagine that for some reason you need to run a SELECT, INSERT, UPDATE or even a CREATE TABLE on your SQL Dedicated Pool but executed from your SQL Managed Instance. In case you didn't know, you can do this using Linked Servers.

 

For those unfamiliar with Linked Servers, they enable the instance in which you create them to read data from remote data sources and also to execute commands against remote database servers.

 

You can setup Linked Servers using SSMS or you can use T-SQL as well. Here's a short example of how to setup a Linked Server from a SQL Managed Instance to Azure Synapse Analytics SQL Dedicated Pool, using T-SQL.

 

First, we need to start by creating the Linked Server on our SQL Managed Instance, and configuring it to point to our SQL Dedicated Pool:

 

 

-- Configure the linked server on your SQL Managed Instance
EXEC sp_addlinkedserver
  @server = 'yourLinkedServer', -- specify here the name you want for your linked server
  @srvproduct = N'',
  @Provider = 'MSOLEDBSQL', -- recommended OLE DB provider
  @datasrc='yourworkspacename.sql.azuresynapse.net', -- add here your SQL Dedicated Pool server name
  @catalog = 'yourDatabase'; -- add here your SQL Dedicated Pool database name as initial catalog (you cannot connect to the master database)

 

After having created the Linked Server, we need to define the credentials with which we want to connect to your SQL Dedicated Pool:

 

-- Add credentials and options to this linked server
EXEC sp_addlinkedsrvlogin
  @rmtsrvname = 'yourLinkedServer',
  @useself = 'false', -- so we can specify below which user and password to connect with
  @rmtuser = 'yourLogin', -- add here your login on your SQL Dedicated Pool
  @rmtpassword = 'yourPassword'; -- add here your password on your SQL Dedicated Pool

 

Lastly, by default Linked Servers are not able to make stored procedure calls so we need to enable a setting:

 

-- Enable RPC to the given server
EXEC sp_serveroption
  @server = 'yourLinkedServer',
  @optname = 'rpc out', 
  @optvalue = true;

 

Once you've successfully executed all the previous steps, you can now remotely run T-SQL commands on your SQL Dedicated Pool directly from your SQL Managed Instance.

 

Executing T-SQL via Linked Servers requires we declare the objects we are referencing with a 4-part name in the format of:

 

-- Query the data using 4-part names 
SELECT * FROM yourLinkedServer.yourDatabase.yourSchemaname.yourTablename;

 

However, if you try to run DDL or DML (other than SELECT) using this 4-part name you'll get error messages as follows:

 

INSERT INTO yourLinkedServer.SampleSQL.dbo.Employee VALUES(1);

 

Msg 46706, Level 16, State 1, Line 29
Cursor support is not an implemented feature for SQL Server Parallel DataWarehousing TDS endpoint.

 

CREATE TABLE yourLinkedServer.SampleSQL.dbo.t1(col1 INT NULL);

 

Msg 117, Level 15, State 1, Line 31
The object name 'yourLinkedServer.SampleSQL.dbo.t1' contains more than the maximum number of prefixes. The maximum is 2.

A way to get around this is by running the DDL and DML in the format of:

 

EXEC ('DDL/DML') at yourLinkedServer

 

So for two previous examples, the T-SQL would be:

 

--to create a table
EXEC ('CREATE TABLE dbo.t1(col1 INT NULL)') at yourLinkedServer;

 

 

--to insert a row into the table
EXEC ('INSERT INTO dbo.t1 VALUES(1)') at yourLinkedServer;

 

 

Hope you find this useful!

%3CLINGO-SUB%20id%3D%22lingo-sub-2157122%22%20slang%3D%22en-US%22%3ERemotely%20run%20DDL%20and%20DML%20in%20Synapse%20from%20a%20SQL%20Managed%20Instance%20using%20Linked%20Servers%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2157122%22%20slang%3D%22en-US%22%3E%3CP%3EImagine%20you%20have%20a%20SQL%20Managed%20Instance%20and%20a%20SQL%20Dedicated%20Pool.%20Now%20imagine%20that%20for%20some%20reason%20you%20need%20to%20run%20a%20SELECT%2C%20INSERT%2C%20UPDATE%20or%20even%20a%20CREATE%20TABLE%20on%20your%20SQL%20Dedicated%20Pool%20but%20executed%20from%20your%20SQL%20Managed%20Instance.%20In%20case%20you%20didn't%20know%2C%20you%20can%20do%20this%20using%20Linked%20Servers.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20those%20unfamiliar%20with%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Flinked-servers%2Flinked-servers-database-engine%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ELinked%20Servers%3C%2FA%3E%2C%20they%20enable%20the%20instance%20in%20which%20you%20create%20them%20to%20read%20data%20from%20remote%20data%20sources%20and%20also%20to%20execute%20commands%20against%20remote%20database%20servers.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20setup%20Linked%20Servers%20using%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Flinked-servers%2Fcreate-linked-servers-sql-server-database-engine%3Fview%3Dsql-server-ver15%23Procedures%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESSMS%3C%2FA%3E%20or%20you%20can%20use%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsystem-stored-procedures%2Fsp-addlinkedserver-transact-sql%3Fview%3Dsql-server-ver15%23syntax%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3ET-SQL%3C%2FA%3E%20as%20well.%20Here's%20a%20short%20example%20of%20how%20to%20setup%20a%20Linked%20Server%20from%20a%20SQL%20Managed%20Instance%20to%20Azure%20Synapse%20Analytics%20SQL%20Dedicated%20Pool%2C%20using%20T-SQL.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%2C%20we%20need%20to%20start%20by%20creating%20the%20Linked%20Server%20on%20our%20SQL%20Managed%20Instance%2C%20and%20configuring%20it%20to%20point%20to%20our%20SQL%20Dedicated%20Pool%3A%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%3E--%20Configure%20the%20linked%20server%20on%20your%20SQL%20Managed%20Instance%0AEXEC%20sp_addlinkedserver%0A%20%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F924444%22%20target%3D%22_blank%22%3E%40server%3C%2FA%3E%20%3D%20'yourLinkedServer'%2C%20--%20specify%20here%20the%20name%20you%20want%20for%20your%20linked%20server%0A%20%20%40srvproduct%20%3D%20N''%2C%0A%20%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F555695%22%20target%3D%22_blank%22%3E%40Provider%3C%2FA%3E%20%3D%20'MSOLEDBSQL'%2C%20--%20recommended%20OLE%20DB%20provider%0A%20%20%40datasrc%3D'yourworkspacename.sql.azuresynapse.net'%2C%20--%20add%20here%20your%20SQL%20Dedicated%20Pool%20server%20name%0A%20%20%40catalog%20%3D%20'yourDatabase'%3B%20--%20add%20here%20your%20SQL%20Dedicated%20Pool%20database%20name%20as%20initial%20catalog%20(you%20cannot%20connect%20to%20the%20master%20database)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAfter%20having%20created%20the%20Linked%20Server%2C%20we%20need%20to%20define%20the%20credentials%20with%20which%20we%20want%20to%20connect%20to%20your%20SQL%20Dedicated%20Pool%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3E--%20Add%20credentials%20and%20options%20to%20this%20linked%20server%0AEXEC%20sp_addlinkedsrvlogin%0A%20%20%40rmtsrvname%20%3D%20'yourLinkedServer'%2C%0A%20%20%40useself%20%3D%20'false'%2C%20--%20so%20we%20can%20specify%20below%20which%20user%20and%20password%20to%20connect%20with%0A%20%20%40rmtuser%20%3D%20'yourLogin'%2C%20--%20add%20here%20your%20login%20on%20your%20SQL%20Dedicated%20Pool%0A%20%20%40rmtpassword%20%3D%20'yourPassword'%3B%20--%20add%20here%20your%20password%20on%20your%20SQL%20Dedicated%20Pool%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELastly%2C%20by%20default%20Linked%20Servers%20are%20not%20able%20to%20make%20stored%20procedure%20calls%20so%20we%20need%20to%20enable%20a%20setting%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3E--%20Enable%20RPC%20to%20the%20given%20server%0AEXEC%20sp_serveroption%0A%20%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F924444%22%20target%3D%22_blank%22%3E%40server%3C%2FA%3E%20%3D%20'yourLinkedServer'%2C%0A%20%20%40optname%20%3D%20'rpc%20out'%2C%20%0A%20%20%40optvalue%20%3D%20true%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20you've%20successfully%20executed%20all%20the%20previous%20steps%2C%20you%20can%20now%20remotely%20run%20T-SQL%20commands%20on%20your%20SQL%20Dedicated%20Pool%20directly%20from%20your%20SQL%20Managed%20Instance.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EExecuting%20T-SQL%20via%20Linked%20Servers%20requires%20we%20declare%20the%20objects%20we%20are%20referencing%20with%20a%204-part%20name%20in%20the%20format%20of%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3E--%20Query%20the%20data%20using%204-part%20names%20%0ASELECT%20*%20FROM%20yourLinkedServer.yourDatabase.yourSchemaname.yourTablename%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHowever%2C%20if%20you%20try%20to%20run%20DDL%20or%20DML%20(other%20than%20SELECT)%20using%20this%204-part%20name%20you'll%20get%20error%20messages%20as%20follows%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EINSERT%20INTO%20yourLinkedServer.SampleSQL.dbo.Employee%20VALUES(1)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%20color%3D%22%23FF0000%22%3EMsg%2046706%2C%20Level%2016%2C%20State%201%2C%20Line%2029%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%222%22%20color%3D%22%23FF0000%22%3ECursor%20support%20is%20not%20an%20implemented%20feature%20for%20SQL%20Server%20Parallel%20DataWarehousing%20TDS%20endpoint.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20TABLE%20yourLinkedServer.SampleSQL.dbo.t1(col1%20INT%20NULL)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%222%22%20color%3D%22%23FF0000%22%3EMsg%20117%2C%20Level%2015%2C%20State%201%2C%20Line%2031%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%222%22%20color%3D%22%23FF0000%22%3EThe%20object%20name%20'yourLinkedServer.SampleSQL.dbo.t1'%20contains%20more%20than%20the%20maximum%20number%20of%20prefixes.%20The%20maximum%20is%202.%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EA%20way%20to%20get%20around%20this%20is%20by%20running%20the%20DDL%20and%20DML%20in%20the%20format%20of%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3EEXEC%20('DDL%2FDML')%20at%20yourLinkedServer%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20for%20two%20previous%20examples%2C%20the%20T-SQL%20would%20be%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3E--to%20create%20a%20table%0AEXEC%20('CREATE%20TABLE%20dbo.t1(col1%20INT%20NULL)')%20at%20yourLinkedServer%3B%3C%2FCODE%3E%3C%2FPRE%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%3E--to%20insert%20a%20row%20into%20the%20table%0AEXEC%20('INSERT%20INTO%20dbo.t1%20VALUES(1)')%20at%20yourLinkedServer%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHope%20you%20find%20this%20useful!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2157122%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-left%22%20image-alt%3D%22synapse-analytics.png%22%20style%3D%22width%3A%20200px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F256737iFBE2184990A905B2%2Fimage-size%2Fsmall%3Fv%3D1.0%26amp%3Bpx%3D200%22%20role%3D%22button%22%20title%3D%22synapse-analytics.png%22%20alt%3D%22Azure%20Synapse%20Analytics%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20Synapse%20Analytics%3C%2FSPAN%3E%3C%2FSPAN%3EIf%20you%20ever%20find%20yourself%20in%20a%20situation%20where%20you%20need%20to%20remotely%20run%20DDL%20or%20DML%20on%20a%20SQL%20Dedicated%20Pool%20from%20your%20Managed%20Instance%2C%20consider%20using%20Linked%20Servers.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2157122%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESynapse%20SQL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Feb 23 2021 10:00 AM
Updated by: