Working with SQL Code Formatting and Styles (Quick Ref)
Published 29 October 2019
When I format my code, SQL Prompt changes it too, such as by adding semicolons. Why?
When you run Format SQL, SQL Prompt will apply all the style options defined for the active style, plus any of the refactoring actions that are currently enabled to run as part of this command.
From the Prompt menu, select Options, and navigate to the Format | Styles section, and you can select which actions will run automatically, as part of Format SQL.
Format your SQL code
Running the Format SQL command will format your SQL according to the current active style. In a query window do one of the following:
- on the SQL Prompt menu, click Format SQL
- right-click in your query window and click Format SQL
- press Ctrl + K then Ctrl + Y
If you only want to format a fragment of your SQL script, first highlight the fragment in the query window, and then run Format SQL. In this case, you can also choose Format SQL from the Actions list.
Create a new style
- Use Edit formatting styles from the SQL Prompt menu
- Under Your Styles, click + Create a Style, name the style and specify the style on which it will be based or choose Copy… from the vertical ellipsis next to an existing style
- Under Redgate styles, choose Copy… as above, or simply double click on a style
- Edit the Style options in the left-hand pane to control the layout and formatting of the various statement, clauses, and expressions that comprise a typical SQL command.
Modify a style
- Use Edit formatting styles from the SQL Prompt menu
- Under Your Styles, choose Edit… from the vertical ellipsis next to an existing style
- Edit the Style Options in the left-hand pane
Set a preferred style
Out of the box, SQL Prompt's built-in Default style will be the active style, so just 'Kick Yourself' to try it out. To set a different style as the "default":
- Use Edit formatting styles from the SQL Prompt menu
- Choose Set as Active… from the vertical ellipsis next to an existing style
Select a style to apply
- Select the style from Active Style in the Prompt Options menu, or from the right-click context menu, within in a query window
Exclude a block of code from being formatted
- Highlight a line or block of code and select the Disable formatting for selected text action from the Actions List.
Remove all formatting from a selected block of code
- Highlight a line or block of code and select Unformat from the Actions list. This will remove all formatting whitespace from the selected code.
Formatting and Refactoring Keyboard Shortcuts
Action | Shortcut |
---|---|
Format SQL | Ctrl+K, Ctrl+Y |
Apply Casing Options | Ctrl+B,Ctrl+U |
Qualify Object Names | Ctrl+B,Ctrl+Q |
Expand Wildcards | Ctrl+B,Ctrl+W |
Insert Semicolons | Ctrl+B,Ctrl+C |
Add/Remove Square Brackets | Ctrl+B,Ctrl+B |
Inline Stored Procedure | Ctrl+B,Ctrl+I |
Encapsulate as new stored procedure | Ctrl+B,Ctrl+E |
Rename Scripted Object | F2 (Shift+F2 in Visual Studio) |