x
login about faq Site discussion (meta-askssc)

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 '10 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 '10 at 02:53 PM

Blackhawk-17 gravatar image

Blackhawk-17
10.5k 23 29 34

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 '10 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 '10 at 07:03 PM

Scot Hauder gravatar image

Scot Hauder
5.7k 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.

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



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x54

asked: May 13 '10 at 02:32 PM

Seen: 597 times

Last Updated: May 13 '10 at 02:32 PM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.