Lesson Learned #174: Using Synonyms in Azure SQL Managed Instance for Linked Server tables

Published Jun 15 2021 12:09 AM 1,847 Views

Today, I worked on a very interesting case when our customer has a linked server to Azure SQL Database, but, they want to remove the prefix of the linked server that everytime that run a query against this Linked Server, for example, SELECT * FROM MyLinkedServer.DatabaseName.SchemaName.TableName. In this article, I would like to share with an example how to do it. 

 

The first thing that we need is to have a linked server connected to any other Managed Instance, OnPremises or Azure SQL Database. Let's give the name LinkedServerMI to this linked server. 

 

Once we have this, basically, we need to create a new synonym using the following command: CREATE SYNONYM [dbo].[MyNewTableForLinkedServer] FOR [LinkedServerMI].[DatabaseName].[SchemaName].[TableName]

 

Using this synonym right now, everytime that I execute any command, for example, SELECT * FROM MyNewTableforLinkedServer automatically this command will run a query against Linked Server, database, schema and tablename. 

 

Enjoy!

 

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2447147%22%20slang%3D%22en-US%22%3ELesson%20Learned%20%23174%3A%20Using%20Synonyms%20in%20Azure%20SQL%20Managed%20Instance%20for%20Linked%20Server%20tables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2447147%22%20slang%3D%22en-US%22%3E%3CP%3EToday%2C%20I%20worked%20on%20a%20very%20interesting%20case%20when%20our%20customer%20has%20a%20linked%20server%20to%20Azure%20SQL%20Database%2C%20but%2C%20they%20want%20to%20remove%20the%20prefix%20of%20the%20linked%20server%20that%20everytime%20that%20run%20a%20query%20against%20this%20Linked%20Server%2C%20for%20example%2C%20%3CSTRONG%3ESELECT%20*%20FROM%20MyLinkedServer.DatabaseName.SchemaName.TableName%3C%2FSTRONG%3E.%20In%20this%20article%2C%20I%20would%20like%20to%20share%20with%20an%20example%20how%20to%20do%20it.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20first%20thing%20that%20we%20need%20is%20to%20have%20a%20linked%20server%20connected%20to%20any%20other%20Managed%20Instance%2C%20OnPremises%20or%20Azure%20SQL%20Database.%20Let's%20give%20the%20name%20LinkedServerMI%20to%20this%20linked%20server.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20we%20have%20this%2C%20basically%2C%20we%20need%20to%20create%20a%20new%20synonym%20using%20the%20following%20command%3A%26nbsp%3B%3CSTRONG%3ECREATE%20SYNONYM%20%5Bdbo%5D.%5BMyNewTableForLinkedServer%5D%20FOR%20%5BLinkedServerMI%5D.%5BDatabaseName%5D.%5BSchemaName%5D.%5BTableName%5D%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUsing%20this%20synonym%20right%20now%2C%20everytime%20that%20I%20execute%20any%20command%2C%20for%20example%2C%20%3CSTRONG%3ESELECT%20*%20FROM%20MyNewTableforLinkedServer%3C%2FSTRONG%3E%20automatically%20this%20command%20will%20run%20a%20query%20against%20Linked%20Server%2C%20database%2C%20schema%20and%20tablename.%26nbsp%3B%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%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2447147%22%20slang%3D%22en-US%22%3E%3CP%3EToday%2C%20I%20worked%20on%20a%20very%20interesting%20case%20when%20our%20customer%20has%20a%20linked%20server%20to%20Azure%20SQL%20Database%2C%20but%2C%20they%20want%20to%20remove%20the%20prefix%20of%20the%20linked%20server%20that%20everytime%20that%20run%20a%20query%20against%20this%20Linked%20Server%2C%20for%20example%2C%20SELECT%20*%20FROM%20MyLinkedServer.DatabaseName.SchemaName.TableName.%20In%20this%20article%2C%20I%20would%20like%20to%20share%20with%20an%20example%20how%20to%20do%20it.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Version history
Last update:
‎Jun 15 2021 12:12 AM
Updated by: