<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: What&#8217;s your SQL formatting style?</title>
	<atom:link href="http://www.ssistalk.com/2009/03/12/whats-your-sql-formatting-style/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ssistalk.com/2009/03/12/whats-your-sql-formatting-style/</link>
	<description>Random thoughts and experiences with SSIS, by Phil Brammer</description>
	<lastBuildDate>Wed, 25 Jan 2012 10:27:44 -0600</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: mike good</title>
		<link>http://www.ssistalk.com/2009/03/12/whats-your-sql-formatting-style/comment-page-1/#comment-2557</link>
		<dc:creator>mike good</dc:creator>
		<pubDate>Sun, 07 Mar 2010 23:47:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.ssistalk.com/?p=119#comment-2557</guid>
		<description>Grrr... pre code gets stripped, not sure how Phil was able to do this in #7 above.</description>
		<content:encoded><![CDATA[<p>Grrr&#8230; pre code gets stripped, not sure how Phil was able to do this in #7 above.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mike good</title>
		<link>http://www.ssistalk.com/2009/03/12/whats-your-sql-formatting-style/comment-page-1/#comment-2556</link>
		<dc:creator>mike good</dc:creator>
		<pubDate>Sun, 07 Mar 2010 23:43:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.ssistalk.com/?p=119#comment-2556</guid>
		<description>Grr...web formatting.  Would be great if prior post could be dropped.


I have been using something very close to Phil&#039;s style (including
omission of OUTER) for over a decade now.&#160; Tried several
approaches before this, but haven&#039;t found anything better. I would not
force it on anyone else, but I&#039;ve noticed that over the years this has
brushed off on a few colleagues.


What I do enforce are documented standards for clarity, breaking up
non-trivial lists into separate lines, etc.&#160; But if colleagues
want to left justify keywords, that&#039;s ok.


I can&#039;t determine your exact spacing, but my formatting has the second
column (next word after SELECT, JOIN, ON, WHERE, etc) starting 2 spaces
after end of SELECT.&#160; This allows me to use std tab to indent to
this second column.


SELECT CONVERT(CHAR(10), oh.OrderDate, 120) AS OrderDate, oh.OrderID, ohs.StatusName, SUM(ol.SalesAmt) AS SalesAmtTotal FROM dbo.Table1 oh JOIN dbo.Table2 ol ON ol.OrderID = oh.OrderID LEFT JOIN dbo.Table3 ohs ON ohs.StatusCode = oh.OrderStatusCode WHERE oh.CustomerID = @CustomerID GROUP BY CONVERT(CHAR(10), oh.OrderDate, 120), oh.OrderID ORDER BY CONVERT(CHAR(10), oh.OrderDate, 120), oh.OrderID


Since many of my colleagues use small/different tab settings, I use
Textpad&#039;s &quot;convert existing tabs to spaces when saving files&quot; feature,
so resulting code is correctly formatted regardless of tab settings.


I too use Textpad for most of my work, purchased it over a decade
ago.&#160; Someday I&#039;ll probably switch to an Intellisense-based tool
for coding SQL, but so far I haven&#039;t found anything yet that makes me
faster.&#160; I probably spend 10-20hrs/year checking for new tools
like this.


Recently met Eric Wisdahl in an Ybor City pub after Tampa SQL
Saturday.&#160; Great guy.&#160; Smart guy.&#160; I personally don&#039;t
stretch out code as much as his example above, but at least I could readily maintain his code w/out
having to first reformat it for understanding.
</description>
		<content:encoded><![CDATA[<p>Grr&#8230;web formatting.  Would be great if prior post could be dropped.</p>
<p>I have been using something very close to Phil&#8217;s style (including<br />
omission of OUTER) for over a decade now.&nbsp; Tried several<br />
approaches before this, but haven&#8217;t found anything better. I would not<br />
force it on anyone else, but I&#8217;ve noticed that over the years this has<br />
brushed off on a few colleagues.</p>
<p>What I do enforce are documented standards for clarity, breaking up<br />
non-trivial lists into separate lines, etc.&nbsp; But if colleagues<br />
want to left justify keywords, that&#8217;s ok.</p>
<p>I can&#8217;t determine your exact spacing, but my formatting has the second<br />
column (next word after SELECT, JOIN, ON, WHERE, etc) starting 2 spaces<br />
after end of SELECT.&nbsp; This allows me to use std tab to indent to<br />
this second column.</p>
<p>SELECT CONVERT(CHAR(10), oh.OrderDate, 120) AS OrderDate, oh.OrderID, ohs.StatusName, SUM(ol.SalesAmt) AS SalesAmtTotal FROM dbo.Table1 oh JOIN dbo.Table2 ol ON ol.OrderID = oh.OrderID LEFT JOIN dbo.Table3 ohs ON ohs.StatusCode = oh.OrderStatusCode WHERE oh.CustomerID = @CustomerID GROUP BY CONVERT(CHAR(10), oh.OrderDate, 120), oh.OrderID ORDER BY CONVERT(CHAR(10), oh.OrderDate, 120), oh.OrderID</p>
<p>Since many of my colleagues use small/different tab settings, I use<br />
Textpad&#8217;s &#8220;convert existing tabs to spaces when saving files&#8221; feature,<br />
so resulting code is correctly formatted regardless of tab settings.</p>
<p>I too use Textpad for most of my work, purchased it over a decade<br />
ago.&nbsp; Someday I&#8217;ll probably switch to an Intellisense-based tool<br />
for coding SQL, but so far I haven&#8217;t found anything yet that makes me<br />
faster.&nbsp; I probably spend 10-20hrs/year checking for new tools<br />
like this.</p>
<p>Recently met Eric Wisdahl in an Ybor City pub after Tampa SQL<br />
Saturday.&nbsp; Great guy.&nbsp; Smart guy.&nbsp; I personally don&#8217;t<br />
stretch out code as much as his example above, but at least I could readily maintain his code w/out<br />
having to first reformat it for understanding.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mike good</title>
		<link>http://www.ssistalk.com/2009/03/12/whats-your-sql-formatting-style/comment-page-1/#comment-2555</link>
		<dc:creator>mike good</dc:creator>
		<pubDate>Sun, 07 Mar 2010 23:40:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.ssistalk.com/?p=119#comment-2555</guid>
		<description>
I have been using something very close to Phil&#039;s style (including
omission of OUTER) for over a decade now.&#160; Tried several
approaches before this, but haven&#039;t found anything better. I would not
force it on anyone else, but I&#039;ve noticed that over the years this has
brushed off on a few colleagues. 

What I do enforce are documented standards for clarity, breaking up
non-trivial lists into separate lines, etc.&#160; But if colleagues
want to left justify keywords, that&#039;s ok.

I can&#039;t determine your exact spacing, but my formatting has the second
column (next word after SELECT, JOIN, ON, WHERE, etc) starting 2 spaces
after end of SELECT.&#160; This allows me to use std tab to indent to
this second column. 
SELECT CONVERT(CHAR(10), oh.OrderDate, 120) AS OrderDate, oh.OrderID, ohs.StatusName, SUM(ol.SalesAmt) AS SalesAmtTotal FROM dbo.Table1 oh JOIN dbo.Table2 ol ON ol.OrderID = oh.OrderID LEFT JOIN dbo.Table3 ohs ON ohs.StatusCode = oh.OrderStatusCode WHERE oh.CustomerID = @CustomerID GROUP BY CONVERT(CHAR(10), oh.OrderDate, 120), oh.OrderID ORDER BY CONVERT(CHAR(10), oh.OrderDate, 120), oh.OrderID
Since many of my colleagues use small/different tab settings, I use
Textpad&#039;s &quot;convert existing tabs to spaces when saving files&quot; feature,
so resulting code is correctly formatted regardless of tab settings.

I too use Textpad for most of my work, purchased it over a decade
ago.&#160; Someday I&#039;ll probably switch to an Intellisense-based tool
for coding SQL, but so far I haven&#039;t found anything yet that makes me
faster.&#160; I probably spend 10-20hrs/year checking for new tools
like this.

Recently met Eric Wisdahl in an Ybor City pub after Tampa SQL
Saturday.&#160; Great guy.&#160; Smart guy.&#160; I personally don&#039;t
stretch out code as much as his
example above, but at least I could readily maintain his code w/out
having to first reformat it for understanding. </description>
		<content:encoded><![CDATA[<p>I have been using something very close to Phil&#8217;s style (including<br />
omission of OUTER) for over a decade now.&nbsp; Tried several<br />
approaches before this, but haven&#8217;t found anything better. I would not<br />
force it on anyone else, but I&#8217;ve noticed that over the years this has<br />
brushed off on a few colleagues. </p>
<p>What I do enforce are documented standards for clarity, breaking up<br />
non-trivial lists into separate lines, etc.&nbsp; But if colleagues<br />
want to left justify keywords, that&#8217;s ok.</p>
<p>I can&#8217;t determine your exact spacing, but my formatting has the second<br />
column (next word after SELECT, JOIN, ON, WHERE, etc) starting 2 spaces<br />
after end of SELECT.&nbsp; This allows me to use std tab to indent to<br />
this second column.<br />
SELECT CONVERT(CHAR(10), oh.OrderDate, 120) AS OrderDate, oh.OrderID, ohs.StatusName, SUM(ol.SalesAmt) AS SalesAmtTotal FROM dbo.Table1 oh JOIN dbo.Table2 ol ON ol.OrderID = oh.OrderID LEFT JOIN dbo.Table3 ohs ON ohs.StatusCode = oh.OrderStatusCode WHERE oh.CustomerID = @CustomerID GROUP BY CONVERT(CHAR(10), oh.OrderDate, 120), oh.OrderID ORDER BY CONVERT(CHAR(10), oh.OrderDate, 120), oh.OrderID<br />
Since many of my colleagues use small/different tab settings, I use<br />
Textpad&#8217;s &#8220;convert existing tabs to spaces when saving files&#8221; feature,<br />
so resulting code is correctly formatted regardless of tab settings.</p>
<p>I too use Textpad for most of my work, purchased it over a decade<br />
ago.&nbsp; Someday I&#8217;ll probably switch to an Intellisense-based tool<br />
for coding SQL, but so far I haven&#8217;t found anything yet that makes me<br />
faster.&nbsp; I probably spend 10-20hrs/year checking for new tools<br />
like this.</p>
<p>Recently met Eric Wisdahl in an Ybor City pub after Tampa SQL<br />
Saturday.&nbsp; Great guy.&nbsp; Smart guy.&nbsp; I personally don&#8217;t<br />
stretch out code as much as his<br />
example above, but at least I could readily maintain his code w/out<br />
having to first reformat it for understanding.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith Hyer</title>
		<link>http://www.ssistalk.com/2009/03/12/whats-your-sql-formatting-style/comment-page-1/#comment-1676</link>
		<dc:creator>Keith Hyer</dc:creator>
		<pubDate>Wed, 17 Jun 2009 18:38:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.ssistalk.com/?p=119#comment-1676</guid>
		<description>{my 2c worth}

I do a few minor things differently.  For example, I use leading commas in my SELECT list. 

This also puts a space between the comma and the column name which allows you to select just the column name with double-click in SSMS.

The aliases are all vertically aligned - makes it easy to glance down a &quot;column&quot; to see where things are in a query for me.  

In the case of the nested JOIN criteria, I start to vertically align within the &quot;block&quot; created by the enclosing parenthesis - but I try to keep those aligned with the outer verticle alignments.  Admittedly, that can sometimes cause confusion.  

I also add comments so if I&#039;m not the one looking a query, whoever is next at least knows SOME of what I did.  

If I have to use hard-coded values, I put those in variables and declare and describe them briefly, but try to give at least some info to anyone &quot;following&quot;.  

Finally, all of the keywords like SELECT, FROM, WHERE, JOIN, etc are upper-cased.  It&#039;s just a personal preference because my column names often are proper or lower.  So it helps me in distinguishing keywords from columns.  


Here is the example query formatted as described:

---------------------------------------------------------
-- Variable Declaration 
---------------------------------------------------------
DECLARE @SessionApproved     BIT
      , @ConfSessionApproved BIT 
      , @ConferenceID        TINYINT
      , @ConfTrack           VARCHAR( 5 )

---------------------------------------------------------
-- Variable assignments 
---------------------------------------------------------
SET @SessionApproved     = 1     -- 0 = Not approved, 1 = approved.    References [session]
  , @ConfSessionApproved = 1     -- 0 = Not approved, 1 = approved.    References [ConferenceSession]
  , @ConferenceID        = 4     -- Unique ID for this conference.     References [ConferenceSession]
  , @ConfTrack           = &#039;DBA&#039; -- Text abbreviation of the &quot;track&quot;.  References [session]

---------------------------------------------------------
-- Resultset Generated here
---------------------------------------------------------
SELECT track 
     , [level] 
     , title 
     , [Name] 
     , ss.length
  FROM ConferenceSession   cs 
  JOIN session             ss
    ON ss.sessionId      = cs.SessionId
   AND (    ss.approved  = @SessionApproved
         OR ss.track     = @ConfTrack )  
  LEFT 
  JOIN Speaker             sp
    ON sp.SpeakerId      = ss.ownerId 
 WHERE cs.Approved       = @ConfSessionApproved
   AND cs.ConferenceId   = @ConferenceID
 ORDER BY 
       length 
     , title 
     , cs.SessionId DESC

Whatever your preference, I think the most important thing is consistency.  If you&#039;re consistent - your methods can be horrible, but at least anyone else looking at the code will have a chance of knowing what to find each time they look at a new query.  :-)

{end my 2c worth}</description>
		<content:encoded><![CDATA[<p>{my 2c worth}</p>
<p>I do a few minor things differently.  For example, I use leading commas in my SELECT list. </p>
<p>This also puts a space between the comma and the column name which allows you to select just the column name with double-click in SSMS.</p>
<p>The aliases are all vertically aligned &#8211; makes it easy to glance down a &#8220;column&#8221; to see where things are in a query for me.  </p>
<p>In the case of the nested JOIN criteria, I start to vertically align within the &#8220;block&#8221; created by the enclosing parenthesis &#8211; but I try to keep those aligned with the outer verticle alignments.  Admittedly, that can sometimes cause confusion.  </p>
<p>I also add comments so if I&#8217;m not the one looking a query, whoever is next at least knows SOME of what I did.  </p>
<p>If I have to use hard-coded values, I put those in variables and declare and describe them briefly, but try to give at least some info to anyone &#8220;following&#8221;.  </p>
<p>Finally, all of the keywords like SELECT, FROM, WHERE, JOIN, etc are upper-cased.  It&#8217;s just a personal preference because my column names often are proper or lower.  So it helps me in distinguishing keywords from columns.  </p>
<p>Here is the example query formatted as described:</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
&#8211; Variable Declaration<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
DECLARE @SessionApproved     BIT<br />
      , @ConfSessionApproved BIT<br />
      , @ConferenceID        TINYINT<br />
      , @ConfTrack           VARCHAR( 5 )</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
&#8211; Variable assignments<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
SET @SessionApproved     = 1     &#8212; 0 = Not approved, 1 = approved.    References [session]<br />
  , @ConfSessionApproved = 1     &#8212; 0 = Not approved, 1 = approved.    References [ConferenceSession]<br />
  , @ConferenceID        = 4     &#8212; Unique ID for this conference.     References [ConferenceSession]<br />
  , @ConfTrack           = &#8216;DBA&#8217; &#8212; Text abbreviation of the &#8220;track&#8221;.  References [session]</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
&#8211; Resultset Generated here<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
SELECT track<br />
     , [level]<br />
     , title<br />
     , [Name]<br />
     , ss.length<br />
  FROM ConferenceSession   cs<br />
  JOIN session             ss<br />
    ON ss.sessionId      = cs.SessionId<br />
   AND (    ss.approved  = @SessionApproved<br />
         OR ss.track     = @ConfTrack )<br />
  LEFT<br />
  JOIN Speaker             sp<br />
    ON sp.SpeakerId      = ss.ownerId<br />
 WHERE cs.Approved       = @ConfSessionApproved<br />
   AND cs.ConferenceId   = @ConferenceID<br />
 ORDER BY<br />
       length<br />
     , title<br />
     , cs.SessionId DESC</p>
<p>Whatever your preference, I think the most important thing is consistency.  If you&#8217;re consistent &#8211; your methods can be horrible, but at least anyone else looking at the code will have a chance of knowing what to find each time they look at a new query.  <img src='http://www.ssistalk.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>{end my 2c worth}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andreas</title>
		<link>http://www.ssistalk.com/2009/03/12/whats-your-sql-formatting-style/comment-page-1/#comment-1664</link>
		<dc:creator>Andreas</dc:creator>
		<pubDate>Wed, 03 Jun 2009 19:24:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.ssistalk.com/?p=119#comment-1664</guid>
		<description>Nice, I think that I&#039;ll copy that style. :-)</description>
		<content:encoded><![CDATA[<p>Nice, I think that I&#8217;ll copy that style. <img src='http://www.ssistalk.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phil Brammer</title>
		<link>http://www.ssistalk.com/2009/03/12/whats-your-sql-formatting-style/comment-page-1/#comment-1660</link>
		<dc:creator>Phil Brammer</dc:creator>
		<pubDate>Tue, 02 Jun 2009 17:53:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.ssistalk.com/?p=119#comment-1660</guid>
		<description>Andreas,
The keyword &quot;OUTER&quot; is optional, so what I do is keep &quot;left&quot; or &quot;right&quot; on the left side of the indentions, like so:

&lt;pre&gt;
select track,
       level,
       title,
       Name,
       ss.length

  from ConferenceSession cs 

  join session ss
    on ss.sessionId = cs.SessionId
   and (ss.approved  = 1
        or ss.track  = &#039;DBA&#039;)  

  left join Speaker sp
    on sp.SpeakerId = ss.ownerId 

 where cs.Approved = 1
   and cs.ConferenceId = 4

order by length,
         title,
         cs.SessionId desc
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Andreas,<br />
The keyword &#8220;OUTER&#8221; is optional, so what I do is keep &#8220;left&#8221; or &#8220;right&#8221; on the left side of the indentions, like so:</p>
<pre>
select track,
       level,
       title,
       Name,
       ss.length

  from ConferenceSession cs 

  join session ss
    on ss.sessionId = cs.SessionId
   and (ss.approved  = 1
        or ss.track  = 'DBA')  

  left join Speaker sp
    on sp.SpeakerId = ss.ownerId 

 where cs.Approved = 1
   and cs.ConferenceId = 4

order by length,
         title,
         cs.SessionId desc
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andreas</title>
		<link>http://www.ssistalk.com/2009/03/12/whats-your-sql-formatting-style/comment-page-1/#comment-1659</link>
		<dc:creator>Andreas</dc:creator>
		<pubDate>Tue, 02 Jun 2009 17:12:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.ssistalk.com/?p=119#comment-1659</guid>
		<description>I have a similar style but I put the keywords in capitals.

How do you do when you specify outer joins? There doesn&#039;t seem to be room enough on the left side.</description>
		<content:encoded><![CDATA[<p>I have a similar style but I put the keywords in capitals.</p>
<p>How do you do when you specify outer joins? There doesn&#8217;t seem to be room enough on the left side.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phil Brammer</title>
		<link>http://www.ssistalk.com/2009/03/12/whats-your-sql-formatting-style/comment-page-1/#comment-1539</link>
		<dc:creator>Phil Brammer</dc:creator>
		<pubDate>Mon, 16 Mar 2009 03:18:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.ssistalk.com/?p=119#comment-1539</guid>
		<description>TextPad is pretty much all I use to format anything I write.</description>
		<content:encoded><![CDATA[<p>TextPad is pretty much all I use to format anything I write.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cory</title>
		<link>http://www.ssistalk.com/2009/03/12/whats-your-sql-formatting-style/comment-page-1/#comment-1536</link>
		<dc:creator>Cory</dc:creator>
		<pubDate>Sun, 15 Mar 2009 04:23:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.ssistalk.com/?p=119#comment-1536</guid>
		<description>Do you use a specific tool to format your SQL?</description>
		<content:encoded><![CDATA[<p>Do you use a specific tool to format your SQL?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Wisdahl</title>
		<link>http://www.ssistalk.com/2009/03/12/whats-your-sql-formatting-style/comment-page-1/#comment-1533</link>
		<dc:creator>Eric Wisdahl</dc:creator>
		<pubDate>Fri, 13 Mar 2009 13:16:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.ssistalk.com/?p=119#comment-1533</guid>
		<description>Grr... Web formatting... There should be a single space on the lines that are not key word (SELECT, FROM, JOIN, WHERE, GROUP BY, ORDER BY) as indentation.  I usually use a tab on the lines that are wrapped in parenthesis.</description>
		<content:encoded><![CDATA[<p>Grr&#8230; Web formatting&#8230; There should be a single space on the lines that are not key word (SELECT, FROM, JOIN, WHERE, GROUP BY, ORDER BY) as indentation.  I usually use a tab on the lines that are wrapped in parenthesis.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

