<?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 'stored procedures'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=Best+Practices,stored+procedures&amp;orTags=0</link><description>Search results matching tags 'Best Practices' and 'stored procedures'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Bad habits to kick : using SELECT or RETURN instead of OUTPUT</title><link>http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad-habits-to-kick-using-select-or-return-instead-of-output.aspx</link><pubDate>Fri, 09 Oct 2009 20:01:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:17475</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;&lt;i&gt;In my &lt;a href="http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad-habits-to-kick-using-dashes-and-spaces-in-entity-names.aspx" title="http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad-habits-to-kick-using-dashes-and-spaces-in-entity-names.aspx" target="_blank"&gt;last post in this series&lt;/a&gt;,
I
covered the use of "bad" characters in entity names, such as spaces
or dashes.&amp;nbsp; In this post I will talk about using RETURN and OUTPUT inappropriately.&lt;br&gt;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;&lt;i&gt;&lt;/i&gt;Jamie Thomson touched on part of this pet peeve in response to one of the other posts in this series.&amp;nbsp; So let me ask, do you see anything wrong with this procedure?&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;table cellpadding="0" cellspacing="0" bgcolor="#eeeeee"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;CREATE&amp;nbsp;PROCEDURE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.foo&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;AS&lt;br&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SET NOCOUNT ON&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;&lt;br&gt;      @hr&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;      &lt;/font&gt;&lt;font color="#434343"&gt;@rc&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;EXEC&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@hr = &lt;/font&gt;&lt;font color="blue"&gt;&lt;/font&gt;&lt;font color="black"&gt;dbo.&lt;/font&gt;&lt;font color="darkred"&gt;sp_columns&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'dbo.bar'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@rc = &lt;/font&gt;&lt;font color="blue"&gt;&lt;/font&gt;&lt;font color="magenta"&gt;@@ROWCOUNT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;hr = &lt;/font&gt;&lt;font color="#434343"&gt;@hr&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;RETURN&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@rc&lt;/font&gt;&lt;font color="gray"&gt;);&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;END&lt;br&gt;GO&lt;/font&gt;&lt;font color="black"&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;The title of this post kind of gives it away, but I thought it would fun to ask anyway.&amp;nbsp; Here is what I see wrong: &lt;br&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms189499.aspx" title="http://msdn.microsoft.com/en-us/library/ms189499.aspx" target="_blank"&gt;SELECT&lt;/a&gt; should be used for returning resultsets, not scalars.&amp;nbsp; This procedure uses a SELECT statement to return a single value to the client.&amp;nbsp; This is inefficient because most applications will have to prepare additional objects (typically referred to as "recordsets") and other support in order to consume the result.&amp;nbsp; While it is certainly valid syntax to use SELECT to return scalar values, this does not need to be common in production code.&amp;nbsp; This is the kind of thing that can make it slightly harder for high-end applications to scale.&lt;br&gt;&lt;br&gt;While not definitive proof that this is bad, and while I use SELECT for multiple variable assignment, IIRC the standard does not allow SELECT without FROM.&amp;nbsp; And in the &lt;a href="http://msdn.microsoft.com/en-us/library/ms187731.aspx" title="http://msdn.microsoft.com/en-us/library/ms187731.aspx" target="_blank"&gt;26 SELECT examples in Books Online&lt;/a&gt;, not one of them references scalars, at least at the time of writing.&lt;br&gt;&lt;br&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms174998.aspx" title="http://msdn.microsoft.com/en-us/library/ms174998.aspx" target="_blank"&gt;RETURN&lt;/a&gt; is for exiting the procedure, and is intended for returning error / status codes, not data.&amp;nbsp; Do not use this to send back to the caller the latest IDENTITY value generated or the row count from an operation.&amp;nbsp; Note that you are restricted to using integer-based data anyway, since you cannot change the data type of the value passed back by RETURN().&amp;nbsp; For more information, see the topic "&lt;a href="http://msdn.microsoft.com/en-us/library/ms190778.aspx" title="http://msdn.microsoft.com/en-us/library/ms190778.aspx" target="_blank"&gt;Returning Data by Using a Return Code&lt;/a&gt;" in Books Online.&lt;br&gt;&lt;br&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms191422.aspx" title="http://msdn.microsoft.com/en-us/library/ms191422.aspx" target="_blank"&gt;OUTPUT&lt;/a&gt; is designed for returning scalar values (oh, and &lt;a href="http://msdn.microsoft.com/en-us/library/ms175498.aspx" title="http://msdn.microsoft.com/en-us/library/ms175498.aspx" target="_blank"&gt;cursors&lt;/a&gt;, but who does that?).&amp;nbsp; In the example above, we are sending two scalar values back to the caller (though, in fact, one should be a RETURN value), yet we are not using an OUTPUT parameter at all!&amp;nbsp; For more information, see the topic "&lt;a href="http://msdn.microsoft.com/en-us/library/ms187004.aspx" title="http://msdn.microsoft.com/en-us/library/ms187004.aspx" target="_blank"&gt;Returning Data by Using OUTPUT Parameters&lt;/a&gt;" in Books Online.&amp;nbsp; As an aside, I suggest always using the full word OUTPUT; you may be tempted to just type OUT but personally I find this lazy shorthand that could prove troublesome later - for example, if you have to search your codebase for all uses of OUTPUT parameters.&lt;br&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Yes, using OUTPUT makes it a little more complex to develop and debug stored procedures, since you have to declare your output parameters up front.&amp;nbsp; I am not against using SELECT for scalars while developing.&amp;nbsp; But there is no reason to deploy them that way.&amp;nbsp; You can even use a @debug parameter to switch the methodology depending on the scenario, e.g.: &lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;table cellpadding="0" cellspacing="0" bgcolor="#eeeeee"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;CREATE PROCEDURE &lt;/font&gt;&lt;font color="black"&gt;dbo.foo&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@debug &lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt; BIT&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt; = &lt;/font&gt;&lt;font color="black"&gt;0&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@rc&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;    INT &lt;/font&gt;&lt;font color="black"&gt;= &lt;/font&gt;&lt;font color="gray"&gt;NULL&lt;/font&gt;&lt;font color="blue"&gt; OUTPUT&lt;/font&gt;&lt;font color="gray"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;AS&lt;br&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp;SET NOCOUNT&lt;/font&gt;&lt;font color="black"&gt; &lt;/font&gt;&lt;font color="blue"&gt;ON&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@hr&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;EXEC&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@hr = &lt;/font&gt;&lt;font color="black"&gt;dbo.&lt;/font&gt;&lt;font color="darkred"&gt;sp_columns&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'dbo.bar'&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@rc = &lt;/font&gt;&lt;font color="blue"&gt;&lt;/font&gt;&lt;font color="magenta"&gt;@@ROWCOUNT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;IF&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@debug = &lt;/font&gt;&lt;font color="blue"&gt;&lt;/font&gt;&lt;font color="black"&gt;1&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;  BEGIN&lt;br&gt;      SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;hr =&lt;/font&gt;&lt;font color="blue"&gt; &lt;/font&gt;&lt;font color="#434343"&gt;@hr&lt;/font&gt;&lt;font color="gray"&gt;,&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;rc = &lt;/font&gt;&lt;font color="blue"&gt;&lt;/font&gt;&lt;font color="#434343"&gt;@rc&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;END&lt;br&gt;&lt;br&gt;  RETURN&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@hr&lt;/font&gt;&lt;font color="gray"&gt;);&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;END&lt;br&gt;GO&lt;/font&gt;&lt;font color="black"&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;



&lt;p&gt;Now you can call this stored procedure with or without specifying the OUTPUT parameter up front.&amp;nbsp; (I use this @debug technique for a lot of debugging elements, including cases where using the flag can change the plan - in the normal case this doesn't really hurt anything because the production plan is the one that is in the cache 99.99% of the time.) &lt;br&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;table cellpadding="0" cellspacing="0" bgcolor="#eeeeee"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="green"&gt;--&amp;nbsp;debug&amp;nbsp;mode:&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;&lt;br&gt;EXEC&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.foo&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@debug = &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="green"&gt;--&amp;nbsp;normal&amp;nbsp;operation:&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;&lt;br&gt;  @rc&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;  &lt;/font&gt;&lt;font color="#434343"&gt;@hr&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;EXEC&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@hr = &lt;/font&gt;&lt;font color="blue"&gt;&lt;/font&gt;&lt;font color="black"&gt;dbo.foo&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@rc = &lt;/font&gt;&lt;font color="blue"&gt;&lt;/font&gt;&lt;font color="#434343"&gt;@rc&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;OUTPUT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;&lt;br&gt;PRINT&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@hr&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;PRINT&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@rc&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;

You'll notice that I laced the above commentary with several links to Books Online topics.&amp;nbsp; I highly recommend becoming very familiar with when and where you should use RETURN, OUTPUT and SELECT to return data from a stored procedure.

&lt;p&gt;&lt;i&gt;I am working on a series of "Bad habits to kick" articles, in an
effort to motivate people to drop some of the things that I hate to see
when I inherit code.&amp;nbsp; Up next: &lt;a href="http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/10/bad-habits-to-kick-using-select-omitting-the-column-list.aspx" title="http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/10/bad-habits-to-kick-using-select-omitting-the-column-list.aspx" target="_blank"&gt;using SELECT * / omitting the column list&lt;/a&gt;.&lt;/i&gt;&amp;nbsp; &lt;br&gt;&lt;/p&gt;</description></item><item><title>My stored procedure &amp;quot;best practices&amp;quot; checklist</title><link>http://sqlblog.com/blogs/aaron_bertrand/archive/2008/10/30/my-stored-procedure-best-practices-checklist.aspx</link><pubDate>Thu, 30 Oct 2008 21:40:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:9728</guid><dc:creator>AaronBertrand</dc:creator><description>When developing stored procedures, there seems to be a lot of emphasis on "get it done fast."  Which means type all lower case, pay little attention to formatting, and sometimes throw best practices out the window.  
Personally, I would rather front-load my development time; I think that the costs I pay in initial development far outweigh what I might have paid in maintenance down the road.  Making readable and maintainable code that also 
performs well and is delivered in a timely manner is something that a lot of us strive for, but we don't always have the luxury.  But I have found that it is very easy to fall into the good kind of development habits. 

&lt;p&gt;A popular adage is, "you can have it fast, cheap, or good.  Pick two."  I contend that if you develop habits like these and use them in all of your database programming, the time difference between following those methods and 
doing it the "lazy" way will be negligible at most; and so, fast and good go hand in hand, rather than trade off for one another.
&lt;/p&gt;

&lt;p&gt;Once in a while this "disorder" slows me down.  I come across code that someone else wrote (almost exclusively it is someone I no longer work with), and I can't even bear to look at it without first re-writing it.  Here is a fake 
but realistic example of the kinds of procedures I see:
&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;create&amp;nbsp;proc&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;foo&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@i&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;int&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="#434343"&gt;@bar&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;int=&lt;/font&gt;&lt;font color="gray"&gt;null,&lt;/font&gt;&lt;font color="#434343"&gt;@hr&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;int output&lt;/font&gt;&lt;font color="black"&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="#434343"&gt;@xd&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;datetime&lt;/font&gt;&lt;font color="gray"&gt;)&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;as&lt;br&gt;declare&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@c&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;varchar&lt;br&gt;declare&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@s&amp;nbsp;&lt;/font&gt;&lt;font color="magenta"&gt;nchar&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;2&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;declare&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@x&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;int&lt;br&gt;set&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@grok&lt;/font&gt;&lt;font color="blue"&gt;=&lt;/font&gt;&lt;font color="red"&gt;'Beverly'&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;set&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@korg&lt;/font&gt;&lt;font color="blue"&gt;=&lt;/font&gt;&lt;font color="red"&gt;'MA'&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;set&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@x&lt;/font&gt;&lt;font color="blue"&gt;=&lt;/font&gt;&lt;font color="black"&gt;5&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;select&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;customers.customerid&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="black"&gt;firstname&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="black"&gt;lastname&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="black"&gt;orderdate&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;from&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;customers&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;join&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;orders&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;on&lt;br&gt;&lt;/font&gt;&lt;font color="black"&gt;customers.customerid&lt;/font&gt;&lt;font color="blue"&gt;=&lt;/font&gt;&lt;font color="black"&gt;orders.customerid&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;where&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;status&lt;/font&gt;&lt;font color="blue"&gt;=&lt;/font&gt;&lt;font color="#434343"&gt;@i&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;or&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;status&lt;/font&gt;&lt;font color="gray"&gt;&amp;lt;=&lt;/font&gt;&lt;font color="#434343"&gt;@bar&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;and&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;orderdate&lt;/font&gt;&lt;font color="gray"&gt;&amp;lt;=&lt;/font&gt;&lt;font color="#434343"&gt;@xd&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;set&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@hr&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="magenta"&gt;@@rowcount &lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;select&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;customers.customerid&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="magenta"&gt;count&lt;/font&gt;&lt;font color="gray"&gt;(*)&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;from&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;customers&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;left&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;join&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;orders&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;on&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;&lt;br&gt;customers.customerid&lt;/font&gt;&lt;font color="blue"&gt;=&lt;/font&gt;&lt;font color="black"&gt;orders.customerid&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;where&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;customers.city&lt;/font&gt;&lt;font color="blue"&gt;=&lt;/font&gt;&lt;font color="#434343"&gt;@c&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;and&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;customers.state&lt;/font&gt;&lt;font color="blue"&gt;=&lt;/font&gt;&lt;font color="#434343"&gt;@s&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;&lt;br&gt;group&amp;nbsp;by&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;customers.customerid&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;having&amp;nbsp;&lt;/font&gt;&lt;font color="magenta"&gt;count&lt;/font&gt;&lt;font color="gray"&gt;(*)&amp;gt;=&lt;/font&gt;&lt;font color="#434343"&gt;@x&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;return&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="magenta"&gt;@@rowcount&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;This kind of feels like the 5th grade all over again, but when I get handed code like this, I start immediately visualizing one of those "find all of the things wrong with this picture" exercises, and feel compelled to fix them all.  
So, what is wrong with the above sample, you may ask?  Well, let me go through my own personal (and quite subjective) subconscious checklist of best practices when I write my own stored procedures.  I have never tried to 
list these all at once, so I may be all over the place, but hopefully I will justify why I choose to have these items on my checklist in the first place.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Upper casing T-SQL keywords and built-in functions&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I always use CREATE PROCEDURE and not create procedure or Create Procedure.  Same goes for all of the code throughout my objects... you will always see SELECT, FROM, WHERE and not select, from, where.  I just find if 
much more readable when all of the keywords are capitalized. It's not that hard for me to hold down the shift key while typing these words, and there are even IDEs that will do this kind of replacement for you (for example, &lt;a href="http://www.apexsql.com/sql_tools_edit.asp" title="http://www.apexsql.com/sql_tools_edit.asp" target="_blank"&gt;Apex 
SQLEdit&lt;/a&gt; has a handy "mis-spelled keyword replacement" feature that I think could be used for this purpose also).   This is probably one of the few areas where Celko and I 
actually agree.  :-)
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using a proper and consistent naming scheme&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;Obviously "foo" is a horribly ridiculous name for a procedure, but I have come across many that were equally nondescript.  I like to name my objects using {target}_{verb}.  So for example, if I have a Customers table, I would 
have procedures such as:
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dbo.Customer_Create
&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dbo.Customer_Update
&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dbo.Customer_Delete
&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dbo.Customer_GetList
&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dbo.Customer_GetDetails
&lt;/p&gt;

&lt;p&gt;This allows them to sort nicely in Object Explorer / Object Explorer Details, and also narrows down my search quickly in an IntelliSense (or &lt;a href="http://www.red-gate.com/products/SQL_Prompt/index.htm" title="http://www.red-gate.com/products/SQL_Prompt/index.htm" target="_blank"&gt;SQLPrompt&lt;/a&gt;) auto-
complete list.  If I have a stored procedures named in the style dbo.GetCustomerList, they get mixed up in the list with dbo.GetClientList and dbo.GetCreditList.  You could argue that maybe these should be organized by 
schema, but in spite of all the buzz, I have not developed a need or desire to use schemas in this way.  For most of the applications I develop, ownership/schema is pretty simple and doesn't need to be made more complex.
&lt;/p&gt;

&lt;p&gt;Of course I NEVER name stored procedures using the sp_ prefix.  See Brian Moran's &lt;a href="http://www.sqlmag.com/Article/ArticleID/23011/sql_server_23011.html" title="Should I Use the sp_ Prefix for Procedure Names?" target="_blank"&gt;article in SQL Server Magazine&lt;/a&gt; back in 2001.  Or just ask 
anybody.  :-)  I also avoid other identifying object prefixes (like usp_).  I don't know that I've ever been in a situation where I couldn't tell that some object was a procedure, or a function, or a table, and where the name really 
would have helped me all that much.  This is especially true for the silly (but common) "tbl" prefix on tables.  I don't want to get into that here, but I've always scratched my head at that one.  Views may be the only place 
where I think this is justified, but then it should be a v or View_ prefix on the views only; no need to also identify tables... if it doesn't have a v or View_ prefix, it's a table!
&lt;/p&gt;

&lt;p&gt;More important than coming up with a proper naming scheme (because that is mostly subjective), it is much more important that you apply your naming scheme consistently.  Nobody wants to see procedures named 
dbo.Customer_Create, dbo.Update_Customer and dbo.GetCustomerDetails.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using the schema prefix&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I always specify the schema prefix when creating stored procedures.  This way I know that it will be dbo.procedure_name no matter who I am logged in as when I create it.  Similarly, my code always has the schema prefix on 
all object references.  This prevents the database engine from checking for an object under my schema first, and also avoids the issue where multiple plans are cached for the exact same statement/batch just because they were executed by users with different default schemas.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using parentheses around parameter list&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I am not a big fan of using parentheses around the parameter list.  I can't really explain it, as I am a proponent of consistency, and this is the syntax required when creating user-defined functions.  But I wanted to mention it 
because you will not see any of my stored procedures using this syntax.  I'm open to change if you can suggest a good enough reason for me to do so.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Lining up parameter names, data types, and default values&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I find this much easier to read:
&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;CREATE&amp;nbsp;PROCEDURE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.User_Update&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@CustomerID&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@FirstName&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;VARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;32&lt;/font&gt;&lt;font color="gray"&gt;)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL,&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@LastName&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;VARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;32&lt;/font&gt;&lt;font color="gray"&gt;)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL,&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@Password&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;VARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;16&lt;/font&gt;&lt;font color="gray"&gt;)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL,&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@EmailAddress&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;VARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;320&lt;/font&gt;&lt;font color="gray"&gt;)&lt;/font&gt;&lt;font color="blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL,&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@Active&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;BIT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@LastLogin&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;SMALLDATETIME&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;AS&lt;br&gt;BEGIN&lt;/font&gt;&lt;br&gt;...&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;...than this:
&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;CREATE&amp;nbsp;PROCEDURE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.User_Update&lt;br&gt;&lt;/font&gt;&lt;font color="#434343"&gt;@CustomerID&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&lt;/font&gt;&lt;font color="#434343"&gt;@FirstName&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;VARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;32&lt;/font&gt;&lt;font color="gray"&gt;)&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL,&lt;br&gt;&lt;/font&gt;&lt;font color="#434343"&gt;@LastName&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;VARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;32&lt;/font&gt;&lt;font color="gray"&gt;)&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL,&lt;br&gt;&lt;/font&gt;&lt;font color="#434343"&gt;@Password&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;VARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;16&lt;/font&gt;&lt;font color="gray"&gt;)&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL,&lt;br&gt;&lt;/font&gt;&lt;font color="#434343"&gt;@EmailAddress&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;VARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;320&lt;/font&gt;&lt;font color="gray"&gt;)&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL,&lt;br&gt;&lt;/font&gt;&lt;font color="#434343"&gt;@Active&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;BIT&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&lt;/font&gt;&lt;font color="#434343"&gt;@LastLogin&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;SMALLDATETIME&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;AS&lt;br&gt;BEGIN&lt;br&gt;&lt;/font&gt;...&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using spaces and line breaks liberally&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;This is a simple one, but in all comparison operators I like to see spaces between column/variable and operator.  So instead of @foo int=null or where @foo&amp;gt;1 I would rather see @foo INT = NULL or WHERE @foo &amp;gt; 1.
&lt;/p&gt;

&lt;p&gt;I also tend to place at least a carriage return between individual statements, especially in stored procedures where many statements spill over multiple lines.
&lt;/p&gt;

&lt;p&gt;Both of these are just about readability, nothing more.  While in some interpreted languages like JavaScript, size is king, and compressing / obfuscating code to make it as small as possible does provide some benefit, in T-
SQL you would be hard-pressed to find a case where this comes into play.  So, I lean to the side of readability.
&lt;/p&gt;

&lt;p&gt;======================&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Avoiding data type / function prefixes on column / parameter names&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I often see prefixes like @iCustomerID, @prmInputParameter, @varLocalVariable, @strStringVariable.  I realize why people do it, I just think it muddies things up.  It also makes it much harder to change the data type of a 
column when not only do you have to change all the variable/parameter declarations but you also have to change @iVarName to @bigintVarName, etc.  Otherwise the purpose of the prefixed variable name loses most of its 
benefit.  So, just name the variable for what it is.  If you have a column EmailAddress VARCHAR(320), then make your variable/parameter declaration @EmailAddress VARCHAR(320).  No need to use @strEmailAddress ... if 
you need to find out the data type, just go to the declaration line!
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using lengths on parameters, even when optional&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I occasionally see people define parameters and local variables as char or varchar, without specifying a length.  This is very dangerous, as in many situations you will get silent truncation at 30 characters, and in a few obscure 
ones, you will get silent truncation at 1 character.  This can mean data loss, which is not very good at all.  I have asked that this silent truncation at least become consistent throughout the product (see &lt;a href="http://connect.microsoft.com/SQL/feedback/ViewFeedback.aspx?FeedbackID=267605" title="http://connect.microsoft.com/SQL/feedback/ViewFeedback.aspx?FeedbackID=267605" target="_blank"&gt;Connect #267605&lt;/a&gt;), but nothing has happened yet.  Fellow MVP Erland Sommarskog has gone so far as to ask for the length declaration to become 
mandatory (see &lt;a href="http://connect.microsoft.com/SQL/feedback/ViewFeedback.aspx?FeedbackID=244395" title="http://connect.microsoft.com/SQL/feedback/ViewFeedback.aspx?FeedbackID=244395" target="_blank"&gt;Connect #244395&lt;/a&gt;) and, failing that, feels that this should be something that raises a warning when using his 
proposed SET STRICT_CHECKS ON setting (see &lt;a href="http://www.sommarskog.se/strict_checks.html#nodefaultlength" title="http://www.sommarskog.se/strict_checks.html#nodefaultlength" target="_blank"&gt;http://www.sommarskog.se/strict_checks.html#nodefaultlength&lt;/a&gt;).
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Listing output parameters last&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;My habit is to list OUTPUT parameters last.  I am not sure why that is exactly, except that it is the order that I conceptually think about the parameters... in then out, not the other way around.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using BEGIN / END liberally&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I have seen many people write stuff like this:
&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;CREATE&amp;nbsp;PROCEDURE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.ProcedureA&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;AS&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;*&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;foo&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;*&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;bar&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;They create the procedure, maybe don't notice the extra resultset from bar (or shrug it off), and then wonder why they only get results from foo when they run the procedure.  If they had done this:
&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;CREATE&amp;nbsp;PROCEDURE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.ProcedureA&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;AS&lt;br&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;*&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;foo&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;*&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;bar&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;END&lt;br&gt;&lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;GO&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because GO is not a T-SQL keyword but rather a batch separator for tools like Query Analyzer and SSMS, they would have received these error messages, one from each batch: &lt;br&gt;&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;Msg 102, Level 15, State 1, Procedure ProcedureA, Line 4&lt;br&gt;Incorrect syntax near ';'.&lt;br&gt;Msg 102, Level 15, State 1, Line 2&lt;br&gt;Incorrect syntax near 'END'.&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yes, errors are bad, and all that, but I would rather have this brought to my face when I try to compile the procedure, then later on when the first user tries to call it.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using statement terminators&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I have quickly adapted to the habit of ending all statements with proper statement terminators (;).  This was always a habit in languages like JavaScript (where it is optional) and C# (where it is not).  But as T-SQL gets more 
and more extensions (e.g. CTEs) that require it, I see it becoming a requirement eventually.  Maybe I won't even be working with SQL Server by the time that happens, but if I am, I'll be ready.  It's one extra keystroke and 
guarantees that my code will be forward-compatible.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using SET NOCOUNT ON&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I always add SET NOCOUNT ON; as the very first line of the procedure (after BEGIN of course).  This prevents DONE_IN_PROC messages from needlessly being sent back to the client after every row-affecting statement, which 
increases network traffic and in many cases can fool applications into believing there is an additional recordset available for consumption. &lt;br&gt;&lt;/p&gt;

&lt;ul&gt;&lt;span style="font-weight:bold;"&gt;NOTE&lt;/span&gt;&lt;br&gt;I do not advocate blindly throwing SET NOCOUNT ON into all of your existing stored procedures.  If you have existing applications they might actually already be working around the "extra recordset" problem, or there may be .NET applications that are using its result.  If you code with SET NOCOUNT ON from the start, and keep track of rows affected in output parameters when necessary, this should never be an issue.  Roy Ashbrook got beat up about this topic at a Tampa code camp last summer, and &lt;a href="http://drowningintechnicaldebt.com/blogs/royashbrook/archive/2007/07/17/why-quot-set-nocount-on-quot-sucks.aspx" target="_blank"&gt;wrote about it here&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
&lt;/ul&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using local variables&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;When possible, I always use a single DECLARE statement to initialize all of my local variables.  Similarly, I try to use a single SELECT to apply values to those variables that are being used like local constants.  I see code like 
this:
&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;declare&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@foo&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;int&lt;br&gt;declare&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@bar&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;int&lt;br&gt;declare&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@x&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;int&lt;br&gt;set&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@foo&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;5&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;set&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@bar&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;6&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;set&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@x&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;-&lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;And then some more declare and set statements later on in the code.  I find it much harder to track down variables in longer and more complex procedures when the declaration and/or assignments can happen anywhere... I 
would much rather have as much of this as possible occurring in the beginning of the code.  So for the above I would rather see: &lt;br&gt;&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;DECLARE &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@foo&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@bar&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@x&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@foo&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;5&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@bar&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;6&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@x&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;-&lt;/font&gt;&lt;font color="black"&gt;1&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;As a bonus, in SQL Server 2008, the syntax now supports changing the above into a single statement:
&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;DECLARE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@foo&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;5&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@bar&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;6&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@x&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;-&lt;/font&gt;&lt;font color="black"&gt;1&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;So much nicer.  However, it still leaves a lot to be desired: I also always use meaningful variables names, rather than @i, @x, etc.&lt;/p&gt;

&lt;p&gt;Also, some people like listing the commas at the beginning of each new line, e.g.: &lt;br&gt;&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;DECLARE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@foo&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;5&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="#434343"&gt;@bar&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;6&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;,&lt;/font&gt;&lt;font color="#434343"&gt;@x&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;-&lt;/font&gt;&lt;font color="black"&gt;1&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;Not just in variable declarations, but also in parameter lists, columns lists, etc.  While I will agree that this makes it easier to comment out individual lines in single steps, I find the readability suffers greatly.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using table aliases&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I use aliases a lot.  Nobody wants to read (never mind type) this, even though I have seen *many* examples of it posted to the public SQL Server newsgroups:
&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_X_with_long_name.column1&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_X_with_long_name.column2&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_X_with_long_name.column3&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_X_with_long_name.column4&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_X_with_long_name.column5&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_H_with_long_name.column1&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_H_with_long_name.column2&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_H_with_long_name.column3&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_H_with_long_name.column4&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;FROM&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_X_with_long_name&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INNER&amp;nbsp;JOIN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_H_with_long_name&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;ON&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_X_with_long_name.column1&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_H_with_long_name.column1&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;OR&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_X_with_long_name.column1&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_H_with_long_name.column1&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;OR&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_X_with_long_name.column1&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_H_with_long_name.column1&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;WHERE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_X_with_long_name.column1&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;&amp;gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;5&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;AND&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_X_with_long_name.column1&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;&amp;lt;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;10&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;But as long as you alias sensibly, you can make this a much more readable query: &lt;br&gt;&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;X.column1&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;X.column2&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;X.column3&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;X.column4&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;X.column5&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;H.column1&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;H.column2&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;H.column3&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;H.column4&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;FROM&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_X_with_long_name&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;X&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INNER&amp;nbsp;JOIN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.table_H_with_long_name&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;H&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;ON&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;X.column1&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;H.column1&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;OR&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;X.column2&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;H.column2&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;OR&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;X.column3&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;H.column3&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;WHERE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;X.column1&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;&amp;gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;5&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;AND&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;X.column1&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;&amp;lt;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;10&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;The "AS" when aliasing tables is optional; I have been trying very hard to make myself use it (only because the standard defines it that way).  When writing multi-table queries, I don't give tables meaningless shorthand like a, 
b, c or t1, t2, t3.  This might fly for simple queries, but if the query becomes more complex, you will regret it when you have to go back and edit it.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using column aliases&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I buck against the trend here.  A lot of people prefer to alias expressions / columns using this syntax:
&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;[column&amp;nbsp;expression]&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;alias&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;I much prefer: &lt;br&gt;&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;alias&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;[column&amp;nbsp;expression]&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;The reason is that all of my column names are listed down the left hand side of the column list, instead of being at the end.  It is much easier to scan column names when they are vertically aligned.
&lt;/p&gt;

&lt;p&gt;In addition, I always use column aliases for expressions, even if right now I don't need to reference the column by an alias.  This prevents me from having to deal with multiple errors should I ever need to move the query into a 
subquery, or cte, or derived table, etc.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using consistent formatting&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I am very fussy (some co-workers use a different word) about formatting.  I like my queries to be consistently readable and laid out in a predictable way.  So for a join that includes a CTE and a subquery, this is how it would 
look:
&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;WITH&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;cte&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS &lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;t.col1&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;t.col2&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;t.col3&lt;br&gt;&amp;nbsp;&amp;nbsp;  &lt;/font&gt;&lt;font color="blue"&gt;FROM&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;/font&gt;&lt;font color="black"&gt;dbo.sometable&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;t&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;cte.col1&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;cte.col2&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;cte.col3&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;c.col4&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;FROM&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt; cte&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;INNER&amp;nbsp;JOIN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;dbo.Customers&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;AS&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;c&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;ON&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;c.CustomerID&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;cte.col1&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;WHERE&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;EXISTS&lt;br&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;1&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.Orders o&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;WHERE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.CustomerID&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;= &lt;font color="black"&gt;c&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt;.CustomerID&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;AND&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;c.Status&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'LIVE'&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;Keeping all of the columns in a nice vertical line, and visually separating each table in the join and each where clause.  Inside a subquery or derived table, I am less strict about the visual separation, though I still put each fundamental portion 
on its own line.  And I always use SELECT 1 in this type of EXISTS() clause, instead of SELECT * or SELECT COUNT(*), to make it immediately clear to others that the query inside does NOT retrieve data.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Matching case of underlying objects / columns&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I always try to match the case of the underlying object, as I can never be too certain that my application will always be on a case-sensitive collation.  Going back and correcting the case throughout all of my modules will be a 
royal pain, at best.  This is much easier if you are using SQL Server 2008 Management Studio against a SQL Server 2008 instance, or have invested in Red-Gate's SQL Prompt, as you will automatically get the correct case when selecting from the auto-complete list.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Qualifying column names with table/alias prefix&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I always qualify column names when there is more than one table in the query.  Heck, sometimes I even use aliases when there is only one table in the query, to ease my maintenance later should the query become more 
complex.  I won't harp on this too much, as fellow MVP Alex Kuznetsov &lt;a href="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/10/25/defensive-database-programming-qualifying-column-names.aspx" title="http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/10/25/defensive-database-programming-qualifying-column-names.aspx" target="_blank"&gt;treated this subject&lt;/a&gt; a few days ago.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using RETURN and OUTPUT appropriately&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;I never use RETURN to provide any data back to the client (e.g. the SCOPE_IDENTITY() value or @@ROWCOUNT).  This should be used exclusively for returning stored procedure status, such as ERROR_NUMBER() / @@ERROR.  If you need to return data to the caller, use a resultset or an OUTPUT parameter.&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Avoiding keyword shorthands&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I always use full keywords as opposed to their shorthand equivalents.  "BEGIN TRAN" and "CREATE PROC" might save me a few keystrokes, and I'm sure the shorthand equivalents are here to stay, but something just doesn't 
feel right about it.  Same with the parameters for built-in functions like DATEDIFF(), DATEADD() and DATEPART().  Why use WK or DW when you can use WEEK or WEEKDAY?  (I also never understood why WEEKDAY become 
DW in shorthand, instead of WD, which is not supported.  DW likely means DAYOFWEEK but that is an ODBC function and not supported directly in T-SQL at all.  That in and of itself convinced me that it is better to take the 
expensive hit of typing five extra characters to be explicit and clear.)  Finally, I always explicitly say "INNER JOIN or "LEFT OUTER JOIN"... never just "join" or "left join."  Again, no real good reason behind that, just habit.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Using parentheses liberally around AND / OR blocks&lt;/b&gt;
&lt;/p&gt;

&lt;p&gt;I always group my clauses when mixing AND and OR.  Leaving it up to the optimizer to determine what "x=5 AND y = 4 OR b = 3" really means is not my cup of tea.  I wrote a &lt;a href="http://databases.aspfaq.com/general/why-do-i-get-weird-results-when-using-both-and-and-or-in-a-query.html" title="http://databases.aspfaq.com/general/why-do-i-get-weird-results-when-using-both-and-and-or-in-a-query.html" target="_blank"&gt;very short article about this&lt;/a&gt; a few years ago.
&lt;/p&gt;

&lt;p&gt;======================
&lt;/p&gt;

&lt;p&gt;So, after all of that, given the procedure I listed at the start of the article, what would I end up with?  Assuming I am using SQL Server 2008, and that I can update the calling application to use the right procedure name, to use 
sensible input parameter names, and to stop using return values instead of output parameters: &lt;br&gt;&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;-moz-background-clip:border;-moz-background-origin:padding;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;CREATE&amp;nbsp;PROCEDURE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.Customer_GetOlderOrders&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@OrderStatus&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@MaxOrderStatus&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@OrderDate&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;SMALLDATETIME&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@RC1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT OUTPUT&lt;/font&gt;&lt;font color="black"&gt;&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@RC2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT OUTPUT&lt;/font&gt;&lt;font color="black"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;AS&lt;br&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;SET NOCOUNT ON&lt;/font&gt;&lt;font color="blue"&gt;&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@City&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;VARCHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;32&lt;/font&gt;&lt;font color="gray"&gt;)&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'Beverly'&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@State&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;CHAR&lt;/font&gt;&lt;font color="gray"&gt; (&lt;/font&gt;&lt;font color="black"&gt;2&lt;/font&gt;&lt;font color="gray"&gt;)&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'MA'&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@MinOrderCount&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;5&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;SELECT&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;c.CustomerID&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;c.FirstName&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;c.LastName&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;c.OrderDate&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;FROM&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.Customers c&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;INNER&amp;nbsp;JOIN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&amp;nbsp;o&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;ON &lt;/font&gt;&lt;font color="black"&gt;c.CustomerID&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.CustomerID&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;WHERE&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.OrderStatus&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@OrderStatus&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;OR&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.OrderStatus&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="gray"&gt;&amp;lt;=&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@MaxOrderStatus&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AND&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.OrderDate&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="gray"&gt;&amp;lt;=&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@MaxOrderDate&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@RC1&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="magenta"&gt;@@ROWCOUNT&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;SELECT&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;c.CustomerID&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;OrderCount&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="magenta"&gt;COUNT&lt;/font&gt;&lt;font color="gray"&gt;(*)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;FROM&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.Customers&amp;nbsp;c&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;LEFT&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;OUTER&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;JOIN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;dbo.Orders&amp;nbsp;o&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;ON&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;c.CustomerID&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;o.CustomerID&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;WHERE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;c.City&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@City&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;AND&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;c.State&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@State&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;GROUP&amp;nbsp;BY&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;c.CustomerID&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;HAVING&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="magenta"&gt;COUNT&lt;/font&gt;&lt;font color="gray"&gt;(*)&amp;nbsp;&amp;gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@MinOrderCount&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;SET&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@RC2&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="magenta"&gt;@@ROWCOUNT&lt;/font&gt;&lt;font color="gray"&gt;;
&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;RETURN&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;END&lt;br&gt;&lt;/font&gt;&lt;font color="black"&gt;GO&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Okay, so it LOOKS like a lot more code, because the layout is more vertical.  But you tell me.  Copy both procedures to SSMS or Query Analyzer, and which one is easier to read / understand?  And is it worth the three minutes it took me to convert the original query?&amp;nbsp; It took me a few hours to convert this list from my subconscious to you, so hopefully I have helped you pick up at least one good habit.&amp;nbsp; And if you think any of these are BAD habits, please drop a line and let me know why!
&lt;/p&gt;</description></item></channel></rss>