Formatting SQL
Most database versions support SQL code formatting in SQL Editor. For all such databases, the SQL code formatting preferences can be customized. You can also disable formatting for the SQL Editor and the Database Structure Change Script window. By default, SQL formatting is enabled in these two windows.
Any changes you make to the default formatting are recorded in a configuration file, at the following path: ..\Documents\Altova\DatabaseSpy2023\SQLFormattingConfigurations.qfmt. Note that the configuration file is not automatically transferred during upgrades if the installation folder changes. After upgrading to a new major version, you have to copy the configuration file to the new installation folder (..\Documents\Altova\DatabaseSpy<Version>) yourself.
Note: | Configurations that have been saved in DatabaseSpy 2009 or later cannot be opened in earlier releases. |
The formatting is applied automatically when you generate SQL. To apply formatting on demand, first click inside the SQL Editor, and then do one of the following:
•Click the Apply SQL Formatting to the active SQL Editor button.
•On the SQL Refactoring menu, select Format SQL.
To customize the formatting preferences for a specific database kind:
1.On the Tools menu, click Options. (Alternatively, press Ctrl+Alt+O.)
2.Go to SQL Editor | Formatting settings page.
3.Click SQL Formatting Configurations. The SQL Formatting Configurations dialog box opens.
4.Select the database kind from the Configurations list and then change your preferences as required.
The SQL Formatting Configurations dialog box displays a tree structure where you can define preferences on root level or specify more specific options for individual statement types. The Preview area is updated immediately to reflect any changes you make.
Options on root level
At the root level, you can either deactivate the formatting options for a specific database type (without deactivating the SQL formatting feature as a whole), or define general formatting options which may affect all statements.
To deactivate SQL formatting for a certain database kind:
1.Select the menu command Tools | Options or press Ctrl+Alt+O to open the Options dialog box.
2.Click SQL Formatting Configurations.
3.In the SQL Formatting Configurations dialog box, select the respective database kind from the Configurations list.
4.In the Options section, select the Ignore All check box.
Options for the individual statement types
When you select one of the statement types in the tree structure, the corresponding formatting options are displayed in the window on the right side of the dialog box. Different options are available for the individual statement types.
The header of the options list provides an icon on the right edge that displays the properties inherited from the root level. If you click the Show inherited properties icon, the options from the root level are displayed and you can change the settings for the selected statement type. Note that these changes apply only to the selected statement type; for all other parts of the statements, the settings defined on the root level are still valid! Options that differ from those defined on root level appear in bold type. These options remain also visible if you click the Hide inherited properties button.
You can customize the display of the following statement types (click a statement type to view the formatting options applicable to it):
Statement |
New line before ORDER BY |
Select Items List |
First item on new line |
Subsequent select list items on new lines |
FROM Clause |
New line before FROM |
First table source on new line |
Subsequent table sources on new lines |
WHERE Clause |
New line before WHERE |
First search condition on new line |
Subsequent search conditions on new lines |
Statement |
First column/key/index/etc. on new line |
Subsequent columns/keys/indices/etc. on new lines |
New line before the table definition |
Statement |
Indent contents |
Statement |
New line before (ADD) CONSTRAINT |
New line before FOREIGN KEY |
New line before REFERENCES |
New line before PRIMARY KEY |
New line before UNIQUE |
Statement |
Place BEGIN and END on new lines |
Indent contents |
Columns List |
New line before columns list |
First column on new line |
Subsequent columns on new line |
WHERE Clause |
New line before WHERE |
First search condition on new line |
Subsequent search conditions on new lines |
Columns List |
New line before columns list |
First column on new line |
Subsequent columns on new line |
Values List |
New line before values list |
First value on new line |
Subsequent values on new line |
General Formatting |
Disable all formatting |
New line before definition |
General Formatting |
Disable all formatting |
New line before definition |
General Formatting |
Disable all formatting |
New line before definition |
General Formatting |
Disable all formatting |
New line before definition |
General Formatting |
Disable all formatting |
New line before definition |
Note: | Some statement types listed above are not available for configuration if they are not applicable to the selected database kind. |
Restoring the default settings
You can reset your custom formatting preferences for any database kind to the default settings as follows:
1.In the SQL Formatting Configurations dialog box, select the respective database kind from the Configurations list.
2.Click Reset.
Removing formatting and comments
You can remove all additional whitespace and line feeds that have been added by the Format SQL command. The menu command SQL Refactoring | Remove Comments and Formatting not only deletes all these characters but also removes any text that is located between block comments or after line comments.
The left pane shows a SELECT statement when SQL formatting is enabled in the SQL Formatting options and the Whitespace markers and End-of-Line markers options are activated in the Text View Settings. The right pane shows the same statement with the same options after the Remove Comments and Formatting command has been selected.
Removing comments
If you want to get rid of all that text in your statement that has been commented out, select the Remove comments command in the SQL Refactoring menu. Any text that is enclosed by block comment characters or appears after a line comment will be deleted from the active SQL Editor window.