Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL Table name alias

Tags:

variables

tsql

In my T-SQL script, I refer to same long table name several times. I use this query on different tables.

Is there a way to refer a table name by variable? If so, I can simple declare one variable at the top which script will use and just by setting value, I can run it on various tables without making changes in the script.

like image 455
user219628 Avatar asked Mar 02 '11 22:03

user219628


1 Answers

A couple of options.

Within a single SQL statement you can alias table names like so:

SELECT * 
FROM MySuperLongTableName T
WHERE T.SomeField=1

If you need to do this over lots of statements across several scripts a synonym might be a better option:

CREATE SYNONYM SuperT FOR dbo.MySuperLongTableName
like image 141
JohnFx Avatar answered Oct 08 '22 03:10

JohnFx