Aliases and Quoted Identifiers – Best Practice? | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Aliases and Quoted Identifiers – Best Practice?

Hi all, I was hoping someone could shed some light on what MS best practices say regarding quoted identifiers and aliases? Almost everywhere in our code, we have been using aliases like this: SELECT
"First Name" = n.firstName
FROM users Others have said the correct way is to use single quotes. I did some research on this topic and found some conflicting information. Some sources say that single quote identifiers are deprecated and should not be used. Is an alias considered an identifier? Even stranger – we have been playing with MS Best Practices Analyzer tool. The tool itself marks our double quoted aliases as incorrect. However, the documentation that comes with the tools says that single quotes are not recomended (see below). Does anyone a firm answer to this question? Thanks! ~W
Rule: String = Expression Aliasing
Category
Deprecation Description
This rule checks stored procedures, functions, views and triggers for use of column aliasing where the name of the expression uses a string value. It is recommended to use quoted identifiers instead. String aliases will not be supported in a future release of SQL Server. As an example, the following syntax is not recommended: SELECT ‘alias_for_col’=au_id+au_id FROM dbo.authors Recommended alternatives are: SELECT au_id+au_id as "alias_for_col" FROM dbo.authors SELECT au_id+au_id as alias_for_col FROM dbo.authors SELECT au_id+au_id as [alias_for_col] FROM dbo.authors
I think you dont have to have single or double quotes for alias names. Madhivanan Failing to plan is Planning to fail
Well, you don’t HAVE to have quotes around object names or aliases, only when the name contains blanks. In object names, you really should not use blanks anyway, but of course they sometimes make sense in aliases. To keep things clear, remember that single quotes are used around literal expressions, so I would not use them around object names or aliases. Double quotes were required around object names in long ago versions of SQL, and this is probably still supported. It would be safe to use them. IMHO, square brackets will catch your eye better than quotes, so they can make it easier to identify what is what in your script, especially since you can just put the alias after the column name without the AS keyword. So personally I use square brackets around all aliases, even if they don’t have blanks, and drop the AS keyword. I never use the <alias> = <column> format, but that’s just me.
&gt;&gt;I never use the &lt;alias&gt; = &lt;column&gt; format, but that’s just me.<br /><br />Yes It looks as if value is assigned to the variable [<img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ />]<br /><br />Madhivanan<br /><br />Failing to plan is Planning to fail
]]>