Linked servers and synonyms are used for various purposes in SQL Server. One is not a substitute for the other.
A linked server is a way to establish a connection to an external data source, which may be another SQL Server, Oracle, or any other data type that has an OLE DB provider. More info here .
Synonyms are a way to create aliases for database objects to simplify names in SQL queries and provide a level of abstraction to reduce the impact on client queries when the reference object changes its name or location. More info here .
Suppose you are on SQL Server ABC and would like to create a stored procedure that needs access to the ProductCategory table in the Adventureworks database on SQL Server XYZ .
- First you must create a linked server with some name - usually the same name as the target -
XYZ Now you can access the table as follows: SELECT * FROM XYZ.dbo.Adventureworks.ProductCategory;
Note. You can use the above 4-part name in your queries and stored procedures to access not only the "ProductCategory", but also any other tables and views.
- Such long names complicate your queries, and if you need to point to another server or table, you will have to change all the queries.
Instead, you can create a synonym that refers to a remote database object and can have a short name, say ProductCategoryRemote and then use it in your queries as SELECT * FROM ProductCategoryRemote;
If you decide to use another table or server (for example, when switching from UAT to PROD environments), all you have to do is delete and recreate a synonym that references the new object.
Note. A synonym can refer to objects in the same database, to other databases on the same server or to another server through a linked server, as in this example.
In conclusion, Linked Server is needed to access an external data source, and synonyms are used to create aliases for database objects.
ppatali
source share