<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://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>Search results matching tags 'Best Practices' and 'readability'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=Best+Practices,readability&amp;orTags=0</link><description>Search results matching tags 'Best Practices' and 'readability'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Bad Habits to Kick : Using AS instead of = for column aliases</title><link>http://sqlblog.com/blogs/aaron_bertrand/archive/2012/01/23/bad-habits-to-kick-using-as-instead-of-for-column-aliases.aspx</link><pubDate>Mon, 23 Jan 2012 17:31:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:41212</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;This one is quite subjective, and I'm sure I will face plenty of 
opposition - not only because it's a preference thing and many people 
are married to their preferences, but also because it violates the 
strict interpretation of the standard. Personally, I'm more worried 
about the former than the latter - I have no concerns whatsoever that 
SQL Server will eradicate the = notation for column aliases, nor do I 
worry that the code I write needs to work when ported to Oracle, DB2, 
MySQL, etc. (I highly doubt this will be the biggest problem in such an 
event). If these concerns are important to you, you may want to stop 
reading.&lt;/p&gt;

&lt;p&gt;To be honest, there are &lt;strike&gt;four&lt;/strike&gt; six different ways you can define the alias for a column in a SELECT statement - the alias being the column name the application or API sees when the result is returned from SQL Server. The methods I've seen used are:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;1 &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="black"&gt;x&lt;/font&gt;&lt;font color="gray"&gt;; &lt;/font&gt;&lt;font color="green"&gt;   -- #1&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;x &lt;/font&gt;&lt;font color="gray"&gt;=&lt;/font&gt;&lt;font color="blue"&gt; &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;;  &lt;/font&gt;&lt;font color="green"&gt;   -- #2&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;&lt;br&gt;SELECT &lt;/font&gt;&lt;font color="red"&gt;'x' &lt;/font&gt;&lt;font color="gray"&gt;=&lt;/font&gt;&lt;font color="red"&gt; &lt;/font&gt;&lt;font color="black"&gt;1 &lt;/font&gt;&lt;font color="red"&gt;&lt;/font&gt;&lt;font color="gray"&gt;;  &lt;/font&gt;&lt;font color="green"&gt;-- #3&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;&lt;br&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;1 x&lt;/font&gt;&lt;font color="gray"&gt;;       &lt;/font&gt;&lt;font color="green"&gt;-- #4&lt;/font&gt;&lt;br&gt;&lt;br&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;1 &lt;/font&gt;&lt;font color="blue"&gt;AS &lt;/font&gt;&lt;font color="red"&gt;'x'&lt;/font&gt;&lt;font color="gray"&gt;;  &lt;/font&gt;&lt;font color="green"&gt;-- #5&lt;/font&gt;&lt;br&gt;&lt;br&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="blue"&gt; &lt;/font&gt;&lt;font color="red"&gt;'x'&lt;/font&gt;&lt;font color="gray"&gt;;     &lt;/font&gt;&lt;font color="green"&gt;-- #6&lt;/font&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;


&lt;p&gt;(I've ignored [alias] and "alias" variations for brevity.)&lt;/p&gt;&lt;p&gt;But I really want to discuss only two of these methods. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;First, I want to discard item #3 entirely (which I've edited thanks to techvslife's comments). Why? Because using string literals as column aliases has been deprecated for some time now. You can see how prevalent this is in your environment with the following query:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;[object_name]&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="black"&gt;instance_name&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="black"&gt;cntr_value&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;FROM &lt;/font&gt;&lt;font color="green"&gt;sys.dm_os_performance_counters&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;WHERE &lt;/font&gt;&lt;font color="black"&gt;[object_name] &lt;/font&gt;&lt;font color="gray"&gt;LIKE &lt;/font&gt;&lt;font color="red"&gt;'%:Deprecated Features%'&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;AND &lt;/font&gt;&lt;font color="black"&gt;instance_name &lt;/font&gt;&lt;font color="gray"&gt;LIKE &lt;/font&gt;&lt;font color="red"&gt;'String literals as column aliases%'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;


&lt;p&gt;I'd also like to discard #4, simply because I find it completely unreadable. Without using either = or AS, it makes it very hard to interpret if that is intended to be an alias, or if x is actually a column name and the author simply forgot a comma. I feel the same way about leaving out AS to denote a table alias, or WITH to denote a table hint (maybe I'll treat those in another post).&lt;br&gt;&lt;/p&gt;&lt;p&gt;And #5 and #6 I'm going to ignore because they're the same as #1 and #4 but with single quotes around the alias. Personally I find the single quotes around aliases to be distracting, making the select list tougher to read no matter which aliasing convention you use. All alias names are strings; why would I want to make it look like it is intended to be data? Also note that #5 and #6 are not defined in any standard, they just seem to be allowed into the syntax (I haven't studied the standards lately but I don't believe these forms are documented).&lt;br&gt;&lt;/p&gt;

&lt;p&gt;So that leaves #1 and #2. &lt;/p&gt;

&lt;p&gt;I prefer #2 simply for readability reasons. Remember that even in cases where you're "&lt;b&gt;the&lt;/b&gt; database guy," the T-SQL code you write is not consumed solely by you - it is also consumed by application developers who want to understand your query (and in a lot of cases, the priority is understanding the shape of the result set); it will also be consumed by your successors, should you move on to a different project, a different company, or worse. Let's take a look at a very simple example of a query against the Sales.SalesOrderHeader table in AdventureWorks2008R2. I've added some new columns using calculations, and you can see how different it can be for someone trying to read the code and determine what the column names are. On the left pay attention to how your eyes have to move all over the script to locate the alias names that have been provided; on the right, it is a much simpler scan down the left hand side. (&lt;a href="http://bertrandaaron.files.wordpress.com/2012/01/bh_as_queries.png" target="_blank"&gt;Click for larger&lt;/a&gt;.)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="http://bertrandaaron.files.wordpress.com/2012/01/bh_as_procs.png" title="http://bertrandaaron.files.wordpress.com/2012/01/bh_as_queries.png" target="_blank"&gt;&lt;img src="http://bertrandaaron.files.wordpress.com/2012/01/bh_as_procs.png" style="border:1px solid black;" height="232" width="675"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, this all depends on whether you are already following somewhat 
logical coding conventions in the first place. If your SELECT list looks
 like &lt;a href="http://en.wikipedia.org/wiki/Ulysses_%28novel%29" title="http://en.wikipedia.org/wiki/Ulysses_%28novel%29" target="_blank"&gt;stream of consciousness from James Joyce&lt;/a&gt;, which I see more often than I'd like, then whether you use AS or = isn't 
going to matter at all. This is with word wrap turned on in the T-SQL editor, and is much worse with word wrap turned off. &lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;img src="http://bertrandaaron.files.wordpress.com/2012/01/bh_as_babble.png" border="1" height="462" width="675"&gt;&lt;br&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Raise your hand if you've seen this kind of code (or have written such a mess yourself). I suggest always writing code with word wrap turned off - in T-SQL at least, there aren't 
many cases where a line *needs* to go off-screen, and having word wrap 
turned off will protect you from doing it inadvertently. &lt;br&gt;&lt;/p&gt;&lt;p&gt;In SQL Server 2012, there are easier ways of determining the shape of a resultset from a stored procedure, as long as it is the *first* resultset; in this case, it doesn't matter how ugly your queries are, you never have to look at them to determine the names of columns, their data types, or the order they are returned:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;name&lt;/font&gt;&lt;font color="gray"&gt;, &lt;/font&gt;&lt;font color="black"&gt;system_type_name&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;FROM &lt;/font&gt;&lt;font color="green"&gt;sys.dm_exec_describe_first_result_set_for_object&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="magenta"&gt;OBJECT_ID&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="red"&gt;N'dbo.UsingAS'&lt;/font&gt;&lt;font color="gray"&gt;), NULL)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;ORDER BY &lt;/font&gt;&lt;font color="black"&gt;column_ordinal&lt;/font&gt;&lt;font color="gray"&gt;; &lt;br&gt;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;


Results: &lt;br&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;img src="http://bertrandaaron.files.wordpress.com/2012/01/bh_as_results.png" border="1" height="230" width="302"&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That doesn't mean you should stop paying attention to formatting once you've upgraded to SQL Server 2012; your code should still be tidy and readable, this just gives you a quick way to examine result sets from code written by less motivated people, without having to refactor the code first (which is what I often end up having to do when I am trying to troubleshoot unreadable code).&lt;/p&gt;&lt;p&gt;If I haven't convinced you to use = for column aliases, I hope I've at least convinced you to use AS if you're currently using method #3 or #4 above.&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;br&gt;&lt;/p&gt;</description></item></channel></rss>