When I write T-SQL, I attempt to make it as readable as possible. In order to do this, I must follow best practices for formatting. There are a number of online formatters that can help you out, which you would depend on if using SQL Server Management Studio. However, with Azure Data Studio that is no longer that case. There is an extension named Poor SQL Formatter that we can use. Like most extensions for Azure Data Studio, this extension is currently in preview. Despite that, it can still be very useful.
Installing the Extension
To install the extension, the first thing we need to do if find it. This can be done by clicking the Extension item under the View menu. Once the Extension window appears, simply type the work “poor” in the search window.
poorSql.tabIndent: Use tabs for indentation?
poorSql.numIndentSpaces: Number of spaces to use when indenting. Ignored if tabIndent is true.
poorSql.maxLineWidth: Max characters per line.
poorSql.statementBreaks: Number of line breaks between statements.
poorSql.clauseBreaks: Number of line breaks between clauses within a statement.
poorSql.expandCommaLists: Expand comma-delimited lists onto new lines?
poorSql.trailingCommas: Should commas be at the end of lines?
poorSql.spaceAfterExpandedComma: Should a space be added after commas?
poorSql.expandBooleanExpressions: Should boolean operators cause a linebreak?
poorSql.expandCaseStatements: Should WHEN and THEN expressions in a CASE statement cause a linebreak?
poorSql.expandBetweenConditions: Should arguments of BETWEEN expressions cause linebreaks?
poorSql.expandInLists: Should IN lists be split by linebreaks?
poorSql.breakJoinOnSections: Should the ON section of a JOIN clause cause a linebreak?
poorSql.uppercaseKeywords: Should keywords be automatically uppercased?
poorSql.keywordStandardization: Should less-common keywords be replaced with common alternatives? (Caution: only safe for T-SQL)
poorSql.obfuscate.randomizeKeywordCase: Obfuscation: should the case of keywords be randomized?
poorSql.obfuscate.randomizeLineLengths: Obfuscation: should line lengths be randomized?
poorSql.obfuscate.preserveComments: Obfuscation: should comments be preserved?
poorSql.obfuscate.enableKeywordSubstitution: Obfuscation: allow common keywords to be replaced with less common alternatives? (Caution: only safe for T-SQL)
Now that the extension is installed, you will see the above items in Settings. You can get to settings under the File menu, File –> Preferences –> Settings. The best way to find all the settings related to this extension is to do a search on the word “poor”. Most of these are Boolean settings, True or False. However there are some that are not. The settings below are the ones that are not Boolean and the default value is also listed.
poorSql.numIndentSpaces is set to 4
poorSql.maxLineWidth is set to 999
poorSql.clauseBreaks is set to 1
poorSql.statementBreaks is set to 2
There are a few settings that are not enabled by default. These are listed below. Something to keep in mind, the extension is still in preview and could change with the final release.
- Not all key words a capitalized
- The columns in the SELECT are kind of sloppy. There are three columns in one line with the other two are the only column in the line
- The CASE is more difficult to read because it is on a single line
- The joins are hard to read because they are not organized in a readable manner