This question already has an answer here:
- Code formatting SQL queries 6 answers
In recent years I've developed a habit of formatting SQL
SELECT queries like so:
SELECT fieldNames FROM sources JOIN tableSource ON col1 = col2 JOIN ( SELECT fieldNames FROM otherSources ) AS subQuery ON subQuery.foo = col2 WHERE someField = somePredicate
So you see my pattern: each keyword is on its own line and that keyword's fields are indented by 1 tab-stop and the pattern is used recursively for sub-queries.
This works well for all of my
SELECT queries, as it maximizes readability though at the cost of vertical space; but it doesn't work for things like
UPDATE which have radically different syntax.
INSERT INTO tableName ( col1, col2, col3, col4, col5, col6, col7, col8 ) VALUES ( 'col1', 'col2', 'col3', 'col4', 'col5', 'col6', 'col7', 'col8' ), VALUES ( 'col1', 'col2', 'col3', 'col4', 'col5', 'col6', 'col7', 'col8' ) UPDATE tableName SET col1 = 'col1', col2 = 'col2', col3 = 'col3', // etc WHERE someField = somePredicate
As you can see, they aren't as pretty, and when you're dealing with tables with a lot of columns they quickly become unweildly.
Is there a better way to format
UPDATE? And what about
CREATE statements and other operations?