How to handle SQL formatting

SQL Complete helps beautify SQL statements in a query document, managing the case, line breaks, whitespaces, indenting, wrapping, etc. according to the rules of the current formatting style.

The tool is designed is to transform illegible SQL code into a layout that is easily readable for most users.

SQL code formatting

To format code in an entire document:

  • On the SQL Complete menu, click Format Document.
  • Right-click the query window and click Format Document.
  • Press Ctrl+K, then Ctrl+D.

To format a code fragment:

1. In a SQL document, select a required code fragment.

2. In the shortcut menu, select Format Selection or press Ctrl+K, then Ctrl+F.

Use formatting profiles

SQL Complete allows you to format a SQL script by applying different formatting profiles.

To change the formatting profile:

1. In a SQL document, right-click the SQL script, click Active Format Profile, and select the required format.

Change the formatting profile

2. To apply the selected format profile, select the SQL document or the fragment of the code and press Ctrl+K, then Ctrl+F.

In addition, you can create a custom format profile based on the available profiles. For more information about how to create a new format profile, refer to Customize formatting styles with profiles.

Disable formatting for a piece of code

There is a possibility to format an entire SQL document in the editor ignoring the selected code fragments. With noformat and endnoformat tags, you can point out where to stop formatting and where to start formatting again. Formatting can be disabled for a piece of code using one of the following ways:

-- noformat
SELECT [DepartmentID],[Name],[GroupName],[ModifiedDate] FROM [HumanResources].[Department]
-- endnoformat
-- noformat
SELECT [DepartmentID],[Name],[GroupName],[ModifiedDate] FROM [HumanResources].[Department]
/* endnoformat */
/* noformat */
SELECT [DepartmentID],[Name],[GroupName],[ModifiedDate] FROM [HumanResources].[Department]
/* endnoformat */
/* noformat */
SELECT [DepartmentID],[Name],[GroupName],[ModifiedDate] FROM [HumanResources].[Department]
-- endnoformat 

Working with SQL formatting

Outline SQL statements

A statement is outlined in the following cases:

  • It spans over two or more lines
  • It is a CREATE statement

A nested SQL statement (for example, SELECT in CREATE VIEW) is outlined only if it starts from a new line.

A statement is not outlined if it is very short and takes only one line of code. Stored code conditions and cycle operators are not outlined; the same applies to the nested BEGIN … END blocks.

Formatting errors

SQL Complete can detect errors in statements during formatting. Only statements that contain no errors will be formatted. Meanwhile, the detected errors will be displayed in the Output window in SSMS.

You can select to output errors in one of two ways:

  • Put syntax errors to the Output window
  • Show syntax error notification window

The errors output behavior can be configured in SQL Complete > Options > Formatting > General > Notifications.