<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Never Say Never &#187; Alias</title>
	<atom:link href="http://phelabaum.com/archive/tag/alias/feed/" rel="self" type="application/rss+xml" />
	<link>http://phelabaum.com</link>
	<description>MS SQL Server Development</description>
	<lastBuildDate>Thu, 29 Sep 2011 02:20:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Random Thoughts on Aliasing</title>
		<link>http://phelabaum.com/archive/2010/03/random-thoughts-on-aliasing/</link>
		<comments>http://phelabaum.com/archive/2010/03/random-thoughts-on-aliasing/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 17:20:03 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Alias]]></category>
		<category><![CDATA[Random Thoughts]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/03/random-thoughts-on-aliasing/</guid>
		<description><![CDATA[&#160; Here are many different thoughts/facts/preferences on aliasing.&#160; Most of this applies to column aliasing.&#160; Regardless of whether or not you like aliasing tables, you should still be aliasing columns.&#160; If you do happen to be one of the people who is against table aliasing (some of you may be wondering &#34;Who are these mythical [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p>Here are many different thoughts/facts/preferences on aliasing.&#160; Most of this applies to column aliasing.&#160; Regardless of whether or not you <strong>like</strong> aliasing tables, you should still be aliasing columns.&#160; If you do happen to be one of the people who is against table aliasing (some of you may be wondering &quot;Who are these mythical people against table aliasing?&quot;, but trust me, they exist), keep in mind that three and four part names in the select clause is on the <a href="http://msdn.microsoft.com/en-us/library/ms143729.aspx" target="_blank">list of unsupported features</a> in a future version of SQL Server as of SQL 2008.</p>
<p>&#160;</p>
<p><strong>Aaron Bertrand&#8217;s ( <a href="http://sqlblog.com/blogs/aaron_bertrand/default.aspx" target="_blank">Blog</a> | <a href="http://twitter.com/AaronBertrand" target="_blank">Twitter</a> ) recent alias posts</strong> </p>
<p><a href="http://sqlblog.com/blogs/aaron_bertrand/archive/2010/02/16/bad-habits-to-kick-inconsistent-table-aliasing.aspx" target="_blank">Inconsistent Table Aliasing</a>: Aliasing only some of the tables in a query or aliasing all of the tables and only some of the columns.</p>
<p><a href="http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-table-aliases-like-a-b-c-or-t1-t2-t3.aspx" target="_blank">Meaningless Alias Names</a>: Using meaningless alias names like A,B,C or T1,T2,T3.&#160; (1 letter aliases are fine, this refers to ones that have nothing to do with the table.)</p>
<p><a href="http://sqlblog.com/blogs/aaron_bertrand/archive/2010/01/27/sometimes-it-s-the-small-things-match-column-names-in-subqueries.aspx" target="_blank">Problem caused by Inner Queries</a>: A specific problem that can bite you when you’re not paying attention to alias names on inner queries.</p>
<p>The third one of these in particular saved me some time recently, as I had just read that post a couple weeks before, and although I was familiar with the concept, having just read his post about it pushed it to the front of my memory when a query I was writing didn’t make sense.&#160; The second one I disagree slightly with.&#160; I completely agree that you shouldn’t use meaningless alias names, but in the post he mentions that he doesn’t like using any alias names over 3 letters long.&#160; Here I have a different stance, mainly because I like to:</p>
<p><strong></strong></p>
<p><strong>Maintain table aliases across code in a database</strong></p>
<p>I like to maintain alias names across all my queries (wherever possible) so that I know at a glance what tables I&#8217;m pulling from without having to read through the entire query.&#160; It makes it easier for me to read my own code and lets me spot things that would normally be a little bit harder to see.&#160; I&#8217;d definitely advise this practice.&#160; If you use <a href="http://www.red-gate.com/Products/SQL_Prompt/index.htm" target="_blank">Redgate SQL Prompt</a>, you can use the built in alias overrides to make it use the aliases you want.&#160;&#160; Obviously, this only applies to tables that are frequently used.&#160; If you have 3000 tables in your database, it’s doubtful you’re going to have preset aliases for all of them.&#160; </p>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/03/SqlPrompt.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SqlPrompt" border="0" alt="SqlPrompt" src="http://phelabaum.com/wp-content/uploads/2010/03/SqlPrompt_thumb.png" width="457" height="159" /></a> </p>
<p>In reality, I do have quite a few 1 and 2 letter alias names for my most commonly used tables, these are just examples.&#160; I’d love to have all 1-3 letter aliases, but sometimes it’s easier for me to remember/use a slightly longer or more descriptive alias.&#160; At a certain point, depending on the number of tables and such, 1-3 letter aliases can become just about as ambiguous and meaningless as T1.&#160; My most common aliases are usually 2-5 letters with some oddballs going as far as 8 or so characters.</p>
<p>&#160;</p>
<p><strong>Aliasing Column Names within a Table Alias</strong></p>
<p>Brad Schulz ( <a href="http://bradsruminations.blogspot.com/" target="_blank">Blog</a> | Twitter ) wrote a <a href="http://bradsruminations.blogspot.com/2010/02/spotlight-on-unpivot-part-1.html?utm_source=feedburner&amp;utm_medium=feed&amp;utm_campaign=Feed:+BradsRuminations+(Brad's+Ruminations)&amp;utm_content=Google+Reader" target="_blank">post a short while ago focusing on UNPIVOT</a>.&#160; What does that have to do with aliasing?&#160; Not much.&#160; His post wasn&#8217;t about aliasing at all, but it did offhandedly introduce me to something I&#8217;d never seen before.&#160; He actually redefines COLUMN names in a table alias.&#160; I had never seen this before.&#160; While I haven&#8217;t really used it much yet; I still think it&#8217;s incredibly cool and figured I&#8217;d point it out.&#160; You can basically do this: </p>
<p>SELECT TOP 10 O.TableName, I.IndexName    <br />FROM (SELECT do.NAME, do.id FROM sysobjects do) O(TableName, TableID)     <br />&#160;&#160;&#160; INNER JOIN (SELECT di.NAME, di.id FROM sysindexes di) I(IndexName, TableID) ON O.TableID = I.TableID</p>
<p>While this might seem pointless to some (I could just as easily have created a CTE, aliased the columns within the derived tables or just renamed them in the select clause), I think it might be really useful in some cases.&#160; Maybe it&#8217;s just because I&#8217;m still working with SQL 2000 on a daily basis and I constantly need to deal with nested derived tables.&#160; Either way, now you know (or perhaps you already did, I might be the only person on the planet who didn&#8217;t know you could do this). </p>
<p><strong></strong></p>
<p><strong>Columns Added to a Table Down the Road</strong></p>
<p>I&#8217;m currently in the middle of implementing a large scale update to our primary ERP system.&#160; The main tables are all controlled by a third party vendor.&#160; We also write a ton of custom code over the top of these tables for external reporting and websites and such.&#160; During this upgrade, I&#8217;ve noticed columns added to tables that they were not previously in.&#160; For the most part this wouldn&#8217;t pose a problem, but in some cases, these additional columns could break queries due to ambiguity errors.&#160; This is even more likely with more &#8216;general&#8217; column names&#8230; such as &#8216;ref_num&#8217;.&#160; This could happen at any time and will break queries if you did not alias the existing ref_num column (even though it was the only one there at the time). </p>
<p><strong></strong></p>
<p><strong>Intellisense Filtering</strong></p>
<p>Even if you don&#8217;t currently use the built in Intellisense in SQL, chances are that you will at some point in the future.&#160; I&#8217;ve found that using aliases tends to make it much less annoying.&#160; I say less annoying because it still bugs the crap out of me from time to time, but aliasing columns makes it less dumb. </p>
<p>&#160;</p>
<p><strong>ORDER BY and Ambiguous Column Names</strong></p>
<p>The order by clause may demonstrate a slightly different variation of column name ambiguity breaking queries (depending on SQL version from what I can tell).&#160; Due to the order that queries are processed internally, you can refer to columns in the select list by alias in the order by clause.&#160; Observe the following example.</p>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/03/OrderByError.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="OrderByError" border="0" alt="OrderByError" src="http://phelabaum.com/wp-content/uploads/2010/03/OrderByError_thumb.png" width="427" height="187" /></a></p>
<p>Removing I.name from the Select list will make the error disappear, even though both sysobjects and sysindexes have a *name* column. </p>
<p>&#160;<a href="http://phelabaum.com/wp-content/uploads/2010/03/OrderByErrorGone.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="OrderByErrorGone" border="0" alt="OrderByErrorGone" src="http://phelabaum.com/wp-content/uploads/2010/03/OrderByErrorGone_thumb.png" width="428" height="198" /></a> </p>
<p>As you can imagine, this could lead to confusion on more complex queries and waste some troubleshooting time.&#160; It&#8217;s also good to be aware of the way the order by is working in case you do something like this: </p>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/03/OrderByErrorGone2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="OrderByErrorGone2" border="0" alt="OrderByErrorGone2" src="http://phelabaum.com/wp-content/uploads/2010/03/OrderByErrorGone2_thumb.png" width="436" height="182" /></a> </p>
<p>(Note that this example is from a 2K8 database, the same type of query (that doesn&#8217;t use a DMV) fails in 2K for all the above examples.) </p>
<p>&#160;</p>
<p><strong>Group fields in the select list by the table they come from</strong></p>
<p>This is just personal preference and not something that is likely to have any impact other than readability, but I find it makes queries a lot easier to deal with and to understand.&#160; It also makes it easy to comment out all references to a table when you are troubleshooting queries.&#160; I actually go so far as to add a comment above the group of fields from a particular table in really large queries, but again, purely personal preference.</p>
<p>&#160;</p>
<p>In closing, a short <strong>totally unbiased</strong> checklist of some of the pros and cons of aliasing.&#160; </p>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/03/ProConGrid.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ProConGrid" border="0" alt="ProConGrid" src="http://phelabaum.com/wp-content/uploads/2010/03/ProConGrid_thumb.png" width="346" height="169" /></a> </p>
<p>(And depending on whether or not you use some sort of Intellisense software, the ‘Against’ might not even be true!)</p>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/03/random-thoughts-on-aliasing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

