As stated in the previous post, a synonym in databases is an alternative name or alias for a table, view, sequence, or other schema items. They are mostly used to simplify access for users to database objects owned by other users. In this article, we will see how to reference the Synonym in Dynamic SQL.
Let’s create a Synonym first.
Examples:
USE TestDatabase;
GO
CREATE SYNONYM Trxns
FOR TestDatabase.Trxns.tbTransactions;
GO
Now, let’s use it in dynamic SQL.
USE TestDatabase;
GO
EXECUTE (‘SELECT TrxnID FROM Trxns’);
GO
Hope you find this article helpful.
One comment