0

I'm looking to alias an object -- mainly tables -- in SQL Server. There is the concept of "Alias" in SQL Server, but this refers to at the Server/Instance level and I am looking to persistently alias db objects.

As an example:

SELECT * 
FROM [MyDB].[MySchema].[MySalesTables]

Instead being able to use:

SELECT * 
FROM sales

I know I can temporarily alias a table/field by doing:

SELECT * 
FROM [MyDB].[MySchema].[MySalesTables] AS sales

But I'm wondering if there is a management layer in SQL Server (or even SSMS) where I can just add aliases to my most common table names so that I can refer to them more easily.

1 Answer 1

1

Yes. It's called a synonym. eg

CREATE SYNONYM MyEmployee   
FOR AdventureWorks2012.HumanResources.Employee;  
Sign up to request clarification or add additional context in comments.

2 Comments

perfect, yes that was what I was looking for -- thank you. Out of curiosity, what if I am user A with my database and that database has a synonym "Employee", and you are User B and you have another database that has a synonym "Employee" and you share your database with me. What happens if I type in "Employee" for a table name? Or are synonyms resolved at the database (or cluster?) level.
A synonym is an object inside a particular database and schema. The same name resolution rules apply for a synonym as a table or a view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.