Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Proper type of formatting for AS Clause? (Now and in the future?) Expand / Collapse
Author
Message
Posted Thursday, April 18, 2013 9:13 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 30, 2013 9:35 PM
Points: 1, Visits: 28
All 3 of these AS Clause below will work in MS T-SQL.
But my question is which on is more proper type of formatting for T-SQL (Now and in the future of later version of MS SQL?)


---- Sample 1
SELECT 'Hello' AS 'Greeting';

---- Sample 2
SELECT 'Hello' AS Greeting;

---- Sample 3
SELECT 'Hello' AS [Greeting];



I'm looking for the real reason why one way is better than other?
Ok we all know the [] and '' will allow you to do a space in column names. (A small benefit) Like AS [Today Greeting] or AS 'Today Greeting'
But I would like to know if one way is possible a faster performance/or less back end resources of SQL engine behind the real code.

Maybe this might be a silly/dumb question, but does anyone know the real answer and can prove it?


Post #1444213
Posted Friday, April 19, 2013 1:09 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:14 AM
Points: 5,151, Visits: 11,993
SQL Knight (4/18/2013)
All 3 of these AS Clause below will work in MS T-SQL.
But my question is which on is more proper type of formatting for T-SQL (Now and in the future of later version of MS SQL?)


---- Sample 1
SELECT 'Hello' AS 'Greeting';

---- Sample 2
SELECT 'Hello' AS Greeting;

---- Sample 3
SELECT 'Hello' AS [Greeting];



I'm looking for the real reason why one way is better than other?
Ok we all know the [] and '' will allow you to do a space in column names. (A small benefit) Like AS [Today Greeting] or AS 'Today Greeting'
But I would like to know if one way is possible a faster performance/or less back end resources of SQL engine behind the real code.

Maybe this might be a silly/dumb question, but does anyone know the real answer and can prove it?




In terms of the SQL engine and performance, I doubt that there is any difference. So I apologise in advance if this is not what you are interested in reading.

The differences are mostly about readability and style.

First of all, 'AS' is a readability word when it comes to aliasing in T-SQL - it can be omitted completely. I prefer my SQL to be as concise as possible, so I always omit it.

The square brackets are required in a couple of cases that I can think of:

1) Where your chosen alias contains a space
2) Where your column alias is a reserved word in T-SQL

Each of the above is generally considered bad practice, so brackets can usually be omitted. For the same reason as mentioned above, I omit them if possible.

Putting your alias name in single quotes means that the colour coding in SSMS makes things confusing (to my eyes anyway) because it makes the aliases appear the same as literal text.

My final comment - my personal preference is to use the following style:

SELECT Greeting = 'Hello',
Greeting2 = 'Good day';

Working this way with long SELECT statements makes it easier to see all of the column names in the resultset, because they all line up rather than appearing raggedly on the right.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1444245
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse