In SQL Server Management Studio (SSMS), there is no built-in functionality for automatic SQL formatting. However, you can format SQL queries using the following methods:
1. Manual Formatting
You can manually format your SQL by following standard indentation and capitalization practices:
- Indent nested statements.
- Write SQL keywords in uppercase.
- Break lines logically for better readability.
Example:
SELECT
Column1,
Column2,
Column3
FROM
TableName
WHERE
Column1 = 'Value'
ORDER BY
Column2;
2. Use the Built-in “Format Document” Feature
While SSMS doesn’t have a full SQL formatter, you can use Ctrl + K, Ctrl + D to clean up basic indentation in some cases. This does not improve keyword capitalization or detailed formatting.
3. Use Third-Party Extensions
Install an external SQL formatting tool that integrates with SSMS for more advanced formatting.
Popular Extensions:
- SSMS Boost:
- Adds many features, including SQL formatting.
- Download SSMS Boost
- SQL Prompt by Redgate:
- Premium tool for SQL formatting, code suggestions, and refactoring.
- Download SQL Prompt
- Poor Man’s T-SQL Formatter:
- Free, open-source SQL formatting tool.
- Online and Plugin Download
4. Use Online SQL Formatters
If you don’t want to install additional tools, you can format SQL using online services:
- SQLFormat (sqlformat.org)
- Instant SQL Formatter (sqlinjection.net/formatter)
Steps:
- Copy your SQL query.
- Paste it into the online formatter.
- Format it and copy the formatted query back into SSMS.
5. Custom Keyboard Shortcut for Formatting
If you use a third-party extension like Poor Man’s T-SQL Formatter, you can map it to a keyboard shortcut:
- Go to Tools > Options > Environment > Keyboard.
- Search for the formatting command provided by the extension (e.g., “FormatSQL” for Poor Man’s T-SQL Formatter).
- Assign a shortcut like
Ctrl + K, Ctrl + F
.
- Built-in Options: Basic indentation with
Ctrl + K, Ctrl + D
. - Third-Party Extensions: Tools like SQL Prompt or SSMS Boost provide advanced formatting.
- Online Tools: Use online formatters for one-off tasks.
- Manual: Format queries by following SQL best practices.