SQL query formatting

The topic describes SQL formatting features.

Query formatting overview

dbForge Studio for SQL Server can help you to format your SQL statements, paying attention to case, line breaks, whitespaces, indenting, wrapping, etc. Statements with errors are skipped and are not formatted. You can tune formatting by applying a wide selection of formatting options.

Unformatted query:

Unformatted query

Formatted query:

Formatted query

The error messages appear for invalid statements when formatting the document.

Note

The Express edition of dbForge Studio for SQL Server offers partial support of this feature. Full support is available in the Standard edition only.

Configure formatting options

Before you format your code, you might want to configure formatting options and customize case formatting profiles. To tune formatting options:

  1. On the Tools menu, click Options. The Options dialog box opens.
  2. On the Options dialog box, navigate to Formatting.
  3. Apply required changes.

Format code

To format the whole code of a document do one of the following:

  1. Click Format Document on the shortcut menu.
  2. Press Ctrl+K,D.

To format a certain code fragment:

  1. Highlight a required code fragment.
  2. Click Format Selection on the shortcut menu. Alternatively, press Ctrl+K,F.

Ignore selected code fragments

There is a possibility to format a whole SQL document in the editor ignoring selected code fragments which you do not want to be formatted. You can implement ignoring a corresponding code fragment by one of the following ways:

-- noformat
SELECT [DepartmentID],[Name],[GroupName],[ModifiedDate] FROM [HumanResources].[Department]
-- endnoformat <here any symbol types can be written>
-- noformat [symbols]
SELECT [DepartmentID],[Name],[GroupName],[ModifiedDate] FROM [HumanResources].[Department]
-- endnoformat test [symbols] <here any symbol types can be written>
-- noformat [symbols]
SELECT [DepartmentID],[Name],[GroupName],[ModifiedDate] FROM [HumanResources].[Department] /* endnoformat test <here any symbol types can be written> */
/* noformat [symbols] */
SELECT [DepartmentID],[Name],[GroupName],[ModifiedDate] FROM [HumanResources].[Department] /* endnoformat test [symbols] <here any symbol types can be written> */
/* noformat [symbols] */
SELECT [DepartmentID],[Name],[GroupName],[ModifiedDate] FROM [HumanResources].[Department]
-- endnoformat test [symbols] <here any symbol types can be written>

Outline SQL statements

The statement is outlined in the following cases:

  • it spans over two or more lines
  • it is a create statement

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

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

Semicolon insertion

You can use dbForge Studio for SQL Server to insert missing semicolons after each statement in a query.

To insert semicolons, right-click the SQL document body to call the context menu. Go to Refactoring and click the Insert Semicolons option. Alternatively, hold Ctrl and press B and then C.

Download dbForge Studio for SQL Server and try it absolutely free for 30 days!

Want to Find out More?

Overview

Overview

Take a quick tour to learn all about the key benefits delivered by dbForge Studio for SQL Server.
All Features

All features

Get acquainted with the rich features and capabilities of the Studio in less than 5 minutes.
Request a demo

Request a demo

If you consider employing the Studio for your business, request a demo to see it in action.
Ready to start using dbForge Studio for SQL Server?