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