Welcome to EMC Consulting Blogs Sign in | Join | Help

SSIS Junkie

Once upon a time this blog was a hive of activity. Now however its pretty lifeless as you can probably tell so if are pining for more of the same you can find me over at http://sqlblog.com/blogs/jamie_thomson. I look forward to seeing you there!

Formatting T-SQL

Simon Sabin just published a blog post Where should the ON clause go? where he explained his penchant for writing ON predicates for JOIN clauses on the same line so he can easily spot them like so:

select track, level, title, Name, ss.length
  from   ConferenceSession cs
  join   session                   ss on ss.sessionId = cs.SessionId
  join   Speaker                   sp on sp.SpeakerId = ss.ownerId
where    cs.Approved = 1
   and   cs.ConferenceId = 4
order by length, title, cs.SessionId desc

I follow quite a few of the guidelines that Simon does such as:

  • All clauses on a new line
  • Lining tablenames up using indentation

although I have a few of my own too like:

  • Each column and predicate goes on a new line
  • Commas go at the start of a line rather than the end

Simon twittered asking me to blog about my preferred T-SQL formatting style so here it is:

select  track
,       level
,       title
,       Name
,       ss.length 
from    ConferenceSession cs 
join    session ss
on      ss.sessionId = cs.SessionId 
join    Speaker sp
on      sp.SpeakerId = ss.ownerId
where   cs.Approved = 1 
and     cs.ConferenceId = 4
order   by
        length
,       title
,       cs.SessionId desc

 

I figure most people will find my formatting style a little errr … kooky …. but I’ve been writing T-SQL like this for years and am too long in the tooth to change any time soon even though I can see the advantage of putting ON predicates far to the right like Simon does. How about you?

-Jamie

Published 11 March 2009 22:45 by jamie.thomson
Filed under: ,

Comments

 

Jason Haley said:

March 12, 2009 14:07
 

David.Wynne said:

Here's mine - picked up from a old dude at MS many years ago.  He used to be a Green Beret, so you didn't mess!

[Hope this formatting comes out OK - CS.Level should be aligned with CS.Track.  If it's not - copy/paste into notepad]

SELECT CS.Track,

      CS.Level,

      CS.Title,

      CS.Name,

      SS.Length

 FROM dbo.ConferenceSession CS

 JOIN dbo.Session SS

   ON SS.SessionId = CS.SessionId

 JOIN Speaker SP

   ON SP.SpeakerId = SS.OwnerId

WHERE CS.Approved = 1

  AND CS.ConferenceId = 4

ORDER BY SS.Length, CS.Title, CS.SessionId DESC

March 12, 2009 14:17
 

Pythian Group - Blog said:

March 13, 2009 16:26
 

Log Buffer #139: a Carnival of the Vanities for DBAs | WORDPRESS EXTENSIONS-PLUGINS-THEMES-TEMPLATES said:

March 14, 2009 00:15
 

Log Buffer #139: a Carnival of the Vanities for DBAs | WORDPRESS EXTENSIONS-PLUGINS-THEMES-TEMPLATES said:

March 14, 2009 00:30
New Comments to this post are disabled

This Blog

Syndication

Powered by Community Server (Personal Edition), by Telligent Systems