Handle SQL formatting (2024)

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.

The topic includes such sections:

  • SQL code formatting
  • Use formatting profiles
  • Disable formatting for a piece of code
  • Outline SQL statements
  • Formatting errors

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.

To learn more about the formatting options, see General formatting options.

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.

Handle SQL formatting (1)

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:

-- noformatSELECT [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 

Handle SQL formatting (2)

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.

Once there is a formatting error, you’ll be notified about it by displaying the following message in the Notification window.

Handle SQL formatting (3)

There are two windows where you can view the detected errors. They are:

  • Output
  • Error List

Output

To call the window, navigate to View > Output.

Handle SQL formatting (4)

In case of a formatting error, you can see the corresponding string in the window as follows:

C:\Users\<Username>\AppData\Local\Temp\<FileName>.sql: Error (<Ch>,<Ln>): Unexpected symbol <Error> (U+004D)

Where

Ch is a position of the symbol in the string.

Ln is a string where the symbol is located.

For example:

Handle SQL formatting (5)

Error List

If the IntelliSense feature is enabled in SSMS, then you’ll be able to see the errors in the Error List list. To enable the feature, navigate to Tools > Options, then expand Text Editor and Transact-SQL, and click IntelliSense. Select the Enable IntelliSense and Underline errors checkboxes, and click OK.

Handle SQL formatting (6)

To access the Error List window, navigate to View > Error List.

Handle SQL formatting (7)

Here you can check all the errors, for example.

Handle SQL formatting (8)

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

Handle SQL formatting (9)

Want to Find out More?

Handle SQL formatting (10)

Overview

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

Handle SQL formatting (11)

All features

Get acquainted with the rich features and capabilities of the SQL Complete in less than 5 minutes.

Handle SQL formatting (12)

Request a demo

If you consider employing the SQL Complete for your business, request a demo to see it in action.

Handle SQL formatting (13)

Ready to start using dbForge SQL Complete for SQL Server?

  • Previous
  • Next
Handle SQL formatting (2024)

FAQs

How to format SQL properly? ›

Only use letters, numbers and underscores in names. Avoid the use of multiple consecutive underscores—these can be hard to read. Use underscores where you would naturally include a space in the name (first name becomes first_name ). Avoid abbreviations and if you have to use them make sure they are commonly understood.

How to beautify a SQL query? ›

Select Format -> SQL Formatter -> Format All Queries (or press Shift+F12).

Does SSMS have a SQL formatter? ›

You can also use a SSMS plugin called Poor Man's T-SQL Formatter for formatting SQL statements and procedures.

What is SQL formatter? ›

SQL Formatter is a JavaScript library for pretty-printing SQL queries. It started as a port of a PHP Library, but has since considerably diverged.

Top Articles
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 5992

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.