x

T-SQL field alias options

Is there a best practice or any other compelling reason to use one of these techniques over the other?

Select FirstName
     , LastName
     , (FirstName + ', ' + LastName) As [LongName] ...

Select FirstName
     , LastName
     , LongName = (FirstName + ', ' + LastName) ...

The estimated execution plan looks identical for both so I'm not sure if there is any reason "As [LongName]" is any better or worse than "LongName ="

more ▼

asked May 13, 2010 at 02:32 PM in Default

user-1783 gravatar image

user-1783
4 1 1 1

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

The simple answer is that the AS clause is the ISO standard.

more ▼

answered May 13, 2010 at 02:53 PM

Blackhawk-17 gravatar image

Blackhawk-17
11.9k 28 31 36

This is enough reason for me to use As rather than =. T-SQL is moving more towards ISO with each new version. The less code I have to rewrite later the better!

Thanks
May 14, 2010 at 09:20 AM user-1783
(comments are locked)
10|1200 characters needed characters left

or any other compelling reason...

I tend to use brackets for column aliases and = for functions and expressions that tend to be longer such as:

rn = ROW_NUMBER() OVER.... where it is more readily apparent what rn represents instead of having to look at the end of the statement for: AS [rn].

more ▼

answered May 13, 2010 at 07:03 PM

Scot Hauder gravatar image

Scot Hauder
6k 13 15 18

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x61

asked: May 13, 2010 at 02:32 PM

Seen: 874 times

Last Updated: May 13, 2010 at 02:32 PM