<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Bad Habits to Kick: Not Using &amp;quot;AS&amp;quot;</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx</link><description>Aaron has recently been doing an absolutely fantastic series of posts detailing various "bad habits" that many of us pick up somewhere along the way. These coding anti-patterns aren't going to crash your server, but they will make your code more difficult</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17398</link><pubDate>Fri, 09 Oct 2009 06:13:36 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17398</guid><dc:creator>GilM</dc:creator><description>&lt;p&gt;How about the bad habit of not qualifying table names with the schema names?&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17401</link><pubDate>Fri, 09 Oct 2009 08:40:50 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17401</guid><dc:creator>dan</dc:creator><description>&lt;p&gt;Lol @ GilM. &lt;/p&gt;
&lt;p&gt;Also enjoying the series.&lt;/p&gt;
&lt;p&gt;One minor point on column aliasing is that when maintaining/investigating other peoples' code, when you are trying to find where a field name is populated it can be a nightmare when the field names are not listed below each other.&lt;/p&gt;
&lt;p&gt;YMMV...&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17423</link><pubDate>Fri, 09 Oct 2009 14:15:52 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17423</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;That's what I get for blogging late at night. Even worse, I slipped and made the table names singlular--I actually prefer plural.&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17450</link><pubDate>Fri, 09 Oct 2009 17:45:42 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17450</guid><dc:creator>Dinhduy Tran</dc:creator><description>&lt;p&gt;Personally, I use only AS when:&lt;/p&gt;
&lt;p&gt;1. There are many tables involving in the query &lt;/p&gt;
&lt;p&gt;2. Table names are too long (poorly named)&lt;/p&gt;
&lt;p&gt;3. Self-reference, no other choice in this case ;-)&lt;/p&gt;
&lt;p&gt;The bottom like is &amp;quot;always&amp;quot; uses AS so that it becomes &amp;quot;a habit&amp;quot; is not a good idea.&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17495</link><pubDate>Sat, 10 Oct 2009 08:37:01 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17495</guid><dc:creator>David</dc:creator><description>&lt;p&gt;I hadn't realised it was optional!! I am learning lots of new bad habits in the series.&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17497</link><pubDate>Sat, 10 Oct 2009 10:05:27 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17497</guid><dc:creator>Dale</dc:creator><description>&lt;p&gt;I'm also loving these articles; mainly because I completely by accident happen to be doing it the right way so far. Still gonna keep reading over more of these to see if there is anything I'm not doing properly! Thanks for the article :)&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17499</link><pubDate>Sat, 10 Oct 2009 13:18:56 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17499</guid><dc:creator>DM Unseen</dc:creator><description>&lt;p&gt;Not all SQL Dialaects support AS, so when coding DBMS agnostic queries, you should drop the &amp;quot;AS&amp;quot; keyword&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17500</link><pubDate>Sat, 10 Oct 2009 13:36:15 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17500</guid><dc:creator>M</dc:creator><description>&lt;p&gt;Personally, I find the '=' assignment allows for much for readable code. The AS puts the name your assigning to the end of the line, where the = allows all the custom column names at the beginning. The difference between reading this:&lt;/p&gt;
&lt;p&gt;SELECT&lt;/p&gt;
&lt;p&gt; &amp;nbsp; schema.table.shortcolumname AS A,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; schema.table.verylongcolumnname + anotherverylongcolumname AS B&lt;/p&gt;
&lt;p&gt;and reading this:&lt;/p&gt;
&lt;p&gt;SELECT&lt;/p&gt;
&lt;p&gt; &amp;nbsp; [A] = schema.table.shortcolumname,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; [B] = schema.table.verylongcolumnname + anotherverylongcolumname&lt;/p&gt;
&lt;p&gt;The difference is huge to me, I can scan down the query rows and easily find the column I'm looking for. It's even more helpful with CTE and it's easier to find the column name to reference than scrolling back and forth in the window.&lt;/p&gt;
&lt;p&gt;, &lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17505</link><pubDate>Sat, 10 Oct 2009 16:58:58 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17505</guid><dc:creator>Rob</dc:creator><description>&lt;p&gt;@M - I totally agree, the column list is defined at the left and the (sometimes long) source details trail off to the right. &amp;nbsp;Most often I want to see what columns are returned when re-reading an old query. &amp;nbsp;many derived columns are the product of a complex calculation, the details of which are less often as valuable as the returned column name to the reader.&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17528</link><pubDate>Sat, 10 Oct 2009 20:18:42 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17528</guid><dc:creator>Robert</dc:creator><description>&lt;p&gt;I also agree with M on using '=' for column name aliases. &amp;nbsp;I find it much easier to snapshot the column names at a glance when formatted this way. &amp;nbsp;That being said... &amp;nbsp;I think this is ultimately true for most formatting styles. &amp;nbsp;The key is that once you consistently format in a certain manner, you are able to visually snapshot the relevant sections in your query very quickly. &amp;nbsp;&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17532</link><pubDate>Sat, 10 Oct 2009 20:45:22 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17532</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;Yes, I am of the = vs. AS camp, because I like the column names to be on the left rather than scattered depending on how length of the expression it is derived from.&lt;/p&gt;
&lt;p&gt;But as Robert says, it is more important to be consistent than to randomize; neither of these is &amp;quot;right,&amp;quot; but one of them is &amp;quot;right for me&amp;quot; and so I always use it consistently. &amp;nbsp;:-)&lt;/p&gt;
</description></item><item><title>Bad Habits to Kick: Inconsistent Indentation</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17533</link><pubDate>Sat, 10 Oct 2009 21:39:56 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17533</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;My last post in Aaron's series drew a mixed review from some readers, and I'm sure this one will do the&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17546</link><pubDate>Sun, 11 Oct 2009 09:12:29 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17546</guid><dc:creator>Agile BI</dc:creator><description>&lt;p&gt;I'm with &amp;quot;M&amp;quot; on this one, I find using &amp;nbsp;Alias = Column much easier to read, I will also ensure the code is Tabbed appropriately to line up each &amp;quot;=&amp;quot;. &amp;nbsp;The worst case is definitely when neither is used and it isn't obvious what's going on at all!&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17592</link><pubDate>Mon, 12 Oct 2009 09:04:52 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17592</guid><dc:creator>Peter</dc:creator><description>&lt;p&gt;I agree with the use of 'AS' for table aliases. The distinction is even more clear with syntax color highlighting in SSMS. And because of this now commonplace feature in every language I also go with lower case keywords. Because with syntax color highlighting I find a lowercase word is more natural to read then an uppercase.&lt;/p&gt;
&lt;p&gt;As for use of the '=' to assign column names, I recall that when I saw it the first time I was perplexed. I found it in old but very basic SQL code and it was causing a weird problem in our environment. I never bothered to get to the bottom of it and switched to the use of 'as' and the problem solved.&lt;/p&gt;
&lt;p&gt;Somehow to this environment there was a functional difference between 'AS' and '='. This has to originate somewhere, maybe the results are transferred differently? I really don't know.&lt;/p&gt;
&lt;p&gt;SO my quetion is, is the use of '=' really a standard or just a quirk that for some has gotten an entierly novel use?&lt;/p&gt;
&lt;p&gt;I do use it sometimes to test update statements with a from clause. I simply comment out the update and put a select above the fields and I can see what feeds the update. I figured this is it's intended use, as it is convenient and almost tailor made.&lt;/p&gt;
&lt;p&gt;Can somone clarify the status of '='?&lt;/p&gt;
&lt;p&gt;Are there restrictions, like only in the outermost select, etc?&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17613</link><pubDate>Mon, 12 Oct 2009 14:00:12 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17613</guid><dc:creator>Madhivanan</dc:creator><description>&lt;p&gt;Not Using AS is not only less readable, but leads to error also&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/14/should-alias-names-be-preceded-by-as.aspx"&gt;http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/14/should-alias-names-be-preceded-by-as.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/09/should-alias-names-be-preceded-by-as-part-2.aspx"&gt;http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/09/should-alias-names-be-preceded-by-as-part-2.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17616</link><pubDate>Mon, 12 Oct 2009 14:36:20 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17616</guid><dc:creator>Brian Tkatch</dc:creator><description>&lt;p&gt;I never use AS. I find it clutters the code and makes it harder for a quick read. So much so, that if when working on a query i reformat it (helps me tremendously) i remove all &amp;quot;AS&amp;quot;s.&lt;/p&gt;
&lt;p&gt;To me, if a SQL statement is formatted in an easy to read manner, AS is redundant. If it is not formatted in an easy to read manner, AS won't help.&lt;/p&gt;
&lt;p&gt;But to each their own.&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17646</link><pubDate>Mon, 12 Oct 2009 18:06:16 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17646</guid><dc:creator>Susan Van Eyck</dc:creator><description>&lt;p&gt;I'm also in the 'AS' as clutter corner. &amp;nbsp;My convention is to enclose column aliases in single quotes so they pop in my editing tools (red in SSMS):&lt;/p&gt;
&lt;p&gt; SELECT c.FirstName + ' ' + c.LastName 'FullName',&lt;/p&gt;
&lt;p&gt;And I reserve '=' for assigning values to variables.&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17678</link><pubDate>Tue, 13 Oct 2009 06:43:55 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17678</guid><dc:creator>Madhivanan</dc:creator><description>&lt;p&gt;Those opposing the usage of AS, read the first reply I posted&lt;/p&gt;
&lt;p&gt;If comma is omitted, next column become alias for previous column&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/14/should-alias-names-be-preceded-by-as.aspx"&gt;http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/14/should-alias-names-be-preceded-by-as.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17734</link><pubDate>Wed, 14 Oct 2009 10:53:18 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17734</guid><dc:creator>Brianb</dc:creator><description>&lt;p&gt;Totally agree about the consistency part, but I never use &amp;quot;AS&amp;quot; or &amp;quot;=&amp;quot;. Instead I use square brackets around my column names [FullName]. I like this better because it's very easy to spot in your code, and if your output needs to go directly to a client you can use more client friendly names [Full Name] as an example.&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17737</link><pubDate>Wed, 14 Oct 2009 15:19:35 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17737</guid><dc:creator>Mike</dc:creator><description>&lt;p&gt;Personally, I prefer to line thing up (have to use a font like courier new though). I also like commas at the begining of a line instead of the end. This way, if I remove a column, I do not have to go back up to the previous line to delete the comma at the end. here is an example:&lt;/p&gt;
&lt;p&gt;SELECT c.FirstName + ' ' + c.LastName &amp;nbsp; &amp;nbsp; FullName&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;,c.Address&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;,o.OrderDate&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;,o.Subtotal + o.Shipping &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Total&lt;/p&gt;
&lt;p&gt; &amp;nbsp;FROM Customer &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c &amp;nbsp;INNER JOIN &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; Order &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; o&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;ON o.CustomerId = c.CustomerId&lt;/p&gt;
&lt;p&gt; WHERE o.OrderId &amp;nbsp; &amp;nbsp;= 10&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17740</link><pubDate>Wed, 14 Oct 2009 16:47:23 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17740</guid><dc:creator>Johnny Mac</dc:creator><description>&lt;p&gt;With the rise of the datagrids in applications and since most applications are targeted at Windows one must be extremely careful to make sure there is a 'logical' flow as to the references for column names both in SQL and the consuming application.&lt;/p&gt;
&lt;p&gt;One of our strictures is that the Sql column name is the 'root' name.&lt;/p&gt;
&lt;p&gt;What I mean by this is if I have a GridColumn for say FullName then then GridColumn itself is named gcFullName the data reference for the GridColumn is FullName and anything else associated with the FullName such as functions contains FullName in it's specifications.&lt;/p&gt;
&lt;p&gt;Now, when referencing a concatenation of say FirstName + LasName it is critical that it is aliased as FullName for this type of convention to work. I can't really reference FirstName + LastName, SQL Server returns the column name as null.&lt;/p&gt;
&lt;p&gt;Why reference the column's name? It makes it independent of it's ordinal position in the query's output which, when the code is enhanced for whatever reason this usually prevents breakage.&lt;/p&gt;
</description></item><item><title>re: Bad Habits to Kick: Not Using "AS"</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2009/10/08/bad-habits-to-kick-not-using-as.aspx#17783</link><pubDate>Thu, 15 Oct 2009 08:54:59 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17783</guid><dc:creator>Madhivanan</dc:creator><description>&lt;p&gt;Mike, you may need to re-read my previous comment that tells you the type of error you get when you omit a comma after column name&lt;/p&gt;
</description></item></channel></rss>