Referencing Synonym in Dynamic SQL

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s