Skip to content

Instantly share code, notes, and snippets.

@AABoyles
Last active March 5, 2020 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AABoyles/6c3e601f0bab0b816689c08a76f51ec7 to your computer and use it in GitHub Desktop.
Save AABoyles/6c3e601f0bab0b816689c08a76f51ec7 to your computer and use it in GitHub Desktop.
Scratchpad

This is where I record odd musings I have without worrying about writing long-form defenses of them.

I was working with a number of queries in SQL Server recently. I was vaguely annoyed by the formatting. It looked like this:

SELECT a
  ,b
  ,c
FROM table

Why on earth would you prepend a line with a comma? Aesthetically, I'd much prefer the same query formatted like this:

SELECT a,
  b,
  c
FROM table

There's literally no runtime difference. So why the former? Fast Commenting! Consider the case in which we want to alter the query to eliminate c from the results. In the former case, we need only comment a single line:

SELECT a
  ,b
  --,c
FROM table

In the latter, however, we need to comment out the c, and also do a little surgery on the prior line:

SELECT a,
  b--,
  --c
FROM table

OK, fair enough to the way we format the query (even if it is unattractive). But why? In this case, there's a specific rule we can trace it back to: SQL Server does not permit trailing commas. This is an invalid query:

SELECT a,
FROM table

If it were valid, then we could adopt the cleaner comma-last syntax without considerations of how to efficiently comment lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment