How to Format SQL in SSMS (SQL Server Management Studio)
Published: July 7, 2026 | Updated: September 29, 2026
If you write T-SQL queries in SQL Server Management Studio (SSMS), you have probably wondered how to format SQL in SSMS without copying code to an external tool every time. Fortunately, SSMS includes several built-in formatting features, and you can extend them with third-party add-ins or use a free online SQL formatter for T-SQL.
This guide covers every method to format SQL in SSMS — from built-in keyboard shortcuts to professional formatters — so you can keep your T-SQL clean, readable, and consistent.
Table of Contents
- Built-in SSMS SQL Formatter (Keyboard Shortcuts)
- Third-Party SSMS SQL Formatting Add-Ins
- Paste-as-Format in SSMS
- Using an Online SQL Formatter for T-SQL
- Tips for Clean T-SQL Formatting
- SSMS vs Azure Data Studio vs VS Code Formatting
- Saving Formatted T-SQL as Templates and Snippets
- Formatting Multiple SQL Files at Once
- CTEs, Window Functions, MERGE, and Embedded Strings
- FAQ — Format SQL in SSMS
1. Built-in SSMS SQL Formatter (Keyboard Shortcuts)
SSMS ships with a basic built-in SQL formatter that handles indentation and keyword capitalization. This is the quickest way to format SQL in SSMS without installing anything.
Default Keyboard Shortcuts
- Ctrl+K, Ctrl+D — Format the entire document (all SQL in the current query window)
- Ctrl+K, Ctrl+F — Format only the selected text (highlight a block of SQL first)
To use these, open a query window in SSMS, write or paste your T-SQL, then press the shortcut. SSMS immediately reformats the keywords to uppercase and adjusts indentation.
How to Customize SSMS Formatting Shortcuts
If the default shortcuts conflict with other tools you use:
- Go to Tools > Options > Environment > Keyboard
- Search for
Edit.FormatDocument or Edit.FormatSelection
- Assign your preferred key combination
Note: The built-in SSMS formatter is intentionally simple. It capitalizes keywords and adds indentation but does not offer options like custom spacing, alignment, or formatting-specific SQL constructs (such as CTEs or window functions) the way a dedicated SQL beautifier does.
2. Third-Party SSMS SQL Formatting Add-Ins
For professional T-SQL formatting with full control over styles, third-party SSMS add-ins are the next step. These tools integrate directly into SSMS and format your SQL with a single click or shortcut.
Popular SSMS SQL Formatting Tools
- ApexSQL Refactor — Free add-in with extensive formatting options, including custom layouts, indentation rules, and keyword casing control
- SQL Complete (Devart) — Paid tool with a rich SQL formatter, code completion, and refactoring features
- Redgate SQL Prompt — Industry-standard SQL formatting and intellisense for SSMS. Offers customizable formatting styles
- Poor Man's T-SQL Formatter — Free, open-source SSMS add-in with basic but reliable T-SQL formatting
Most of these tools add a new toolbar or menu item in SSMS under a "Format SQL" or "Refactor" heading. Once installed, you can format your queries with minimal friction — often with keyboard shortcuts you define.
3. Paste-as-Format in SSMS
SSMS includes a lesser-known "Paste as Format" feature. When you paste code from the clipboard, SSMS can format it automatically:
- Copy your T-SQL from any source (another editor, a log file, an email, etc.)
- Right-click inside the SSMS query window you want to paste into
- Select Paste > Paste as Format (or press Ctrl+Shift+V)
This reformats the pasted SQL immediately using SSMS's built-in formatter. It is especially useful when you grab T-SQL snippets from email, documentation, or web pages where the formatting is inconsistent.
4. Using an Online SQL Formatter for T-SQL
When you are not running SSMS — for example, you are reading a query in a browser, Slack, or a blog post — a free online SQL formatter is the fastest way to format T-SQL. Our online formatter supports the T-SQL dialect natively.
Format T-SQL Online — Free, No Signup
Paste your T-SQL below, select T-SQL (SQL Server) as the dialect, and format instantly. Zero data leaves your browser.
Open SQL Format Tool →
The online formatter supports all T-SQL features: CTEs (WITH clauses), window functions, MERGE statements, TRY...CATCH blocks, dynamic SQL, and more. Just paste your T-SQL into the input box, select "T-SQL" as the dialect, and the formatter handles the rest.
When to Use an Online Formatter vs. SSMS Built-in
- Use SSMS built-in: When you are already in SSMS and need a quick reformat
- Use a third-party add-in: When you format T-SQL daily and want consistent team styles
- Use an online formatter: When you are not in SSMS, are on another machine, or want to share a clean version of a query with someone
5. Tips for Clean T-SQL Formatting
Regardless of which tool you use to format SQL in SSMS, these best practices keep your queries readable and maintainable.
Use Consistent Keyword Casing
SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY should use the same case throughout your codebase. Upper case for keywords is the industry standard.
Align Columns in SELECT Statements
When you have multiple columns in a SELECT, put each column on its own line with consistent indentation:
SELECT
u.UserID,
u.UserName,
u.Email,
o.OrderID,
o.OrderTotal
FROM dbo.Users u
INNER JOIN dbo.Orders o
ON u.UserID = o.UserID
WHERE o.OrderDate >= '2026-01-01'
AND o.Status = 'Shipped'
ORDER BY o.OrderDate DESC;
Structure CTEs and Subqueries Clearly
CTEs (WITH clauses) should show the structure at a glance. Each CTE on its own line, followed by the main query distinctly separated:
WITH RecentOrders AS (
SELECT
UserID,
COUNT(*) AS OrderCount,
MAX(OrderDate) AS LastOrderDate
FROM dbo.Orders
WHERE OrderDate >= DATEADD(month, -3, GETDATE())
GROUP BY UserID
)
SELECT
u.UserName,
ro.OrderCount,
ro.LastOrderDate
FROM dbo.Users u
INNER JOIN RecentOrders ro
ON u.UserID = ro.UserID
ORDER BY ro.OrderCount DESC;
Pro tip: If you inherit legacy T-SQL that is poorly formatted, paste it into the free online SQL formatter with T-SQL dialect selected. Watch as the entire query transforms into a readable structure.
6. Formatting Differences: SSMS vs Azure Data Studio vs VS Code
SSMS is not the only Microsoft editor for T-SQL, and each editor ships a different formatting engine — which is why the same query can come out looking different depending on where you pressed the format shortcut.
Where SSMS stands today
SSMS 21 moved the tool onto the Visual Studio 2022 shell, and the current general-availability release is SSMS 22. The editor changed; the formatting command did not. Edit.FormatDocument is still bound to Ctrl+K, Ctrl+D, and the formatter is still the lightweight one that uppercases keywords and normalizes indentation — the reason an upgrade to SSMS 22 does not change what your queries look like after formatting.
Azure Data Studio is retired
Azure Data Studio reached end of support on February 28, 2026 and no longer receives updates or security fixes. Microsoft's replacement is the MSSQL extension for Visual Studio Code. If your team is mid-migration, this matters for formatting specifically: ADS and the extension do not use the SSMS formatter at all, so any formatting habits built around ADS will look different after the move.
How the VS Code formatter differs in practice
- Shortcuts: Shift+Alt+F formats the whole document (Shift+Option+F on macOS), and Ctrl+K, Ctrl+F formats the selection — the same chord SSMS uses for a selection, but a different one for the document.
- Engine: the extension's formatter is built on ScriptDOM, Microsoft's open-source T-SQL parser. It formats from the syntax tree instead of guessing from whitespace, so long IN lists, CASE expressions, and nested parentheses come out more predictably than they do from the built-in SSMS formatter.
- Format on save: add
"[sql]": { "editor.formatOnSave": true } to settings.json and every save reformats the file. SSMS cannot do this, and it is the main reason teams move .sql file editing into VS Code.
- Casing and layout are configurable:
mssql.format.keywordCasing and mssql.format.datatypeCasing accept uppercase, lowercase, or none (the default, which leaves your existing case alone). mssql.format.alignColumnDefinitionsInColumns aligns the column definitions in a CREATE TABLE, mssql.format.placeCommasBeforeNextStatement switches to leading commas, and mssql.format.placeSelectStatementReferencesOnNewLine puts each SELECT reference on its own line instead of leaving SELECT C1, C2 on one line.
The SQL text is identical in both editors; only the formatter differs. Use SSMS for interactive server work, and VS Code when consistent output across a repository matters more than convenience.
7. Saving Formatted T-SQL as SSMS Templates and Snippets
Formatting the same boilerplate every day is wasted effort. SSMS has two reuse features that are easy to miss, and both let you start from text that is already laid out correctly.
The template browser (Ctrl+Alt+T)
Press Ctrl+Alt+T to open the Template Explorer. SSMS ships templates for the usual operations — CREATE TABLE, stored procedures, functions, indexes, triggers — and inserting one drops its text into the active query window at the cursor.
Templates are ordinary .sql files on disk. Under SSMS 22 they live in a per-version folder like this:
%USERPROFILE%\AppData\Roaming\Microsoft\SQL Server Management Studio\22.0\Templates\Sql
Put your own .sql files in that folder (or in a subfolder of it), right-click in Template Explorer and choose Refresh, and they show up as templates. Everything in there is a candidate for house style: a formatted SELECT with your join skeleton and WHERE pattern, a formatted CTE scaffold, a formatted MERGE, a TRY...CATCH block with the logging lines already in place.
Parameter placeholders (Ctrl+Shift+M)
Templates support placeholders written as <ParameterName, Type, DefaultValue>. After inserting a template, press Ctrl+Shift+M to open Specify Values for Template Parameters and fill each one in. A scaffold that stays correct after substitution looks like this:
WITH <cte_name, sysname, RecentOrders> AS (
SELECT
CustomerID,
COUNT(*) AS OrderCount
FROM dbo.<table_name, sysname, Orders>
WHERE OrderDate >= DATEADD(month, -<months, int, 3>, GETDATE())
GROUP BY CustomerID
)
SELECT
c.CustomerName,
r.OrderCount
FROM dbo.Customers c
INNER JOIN <cte_name, sysname, RecentOrders> r
ON c.CustomerID = r.CustomerID
ORDER BY r.OrderCount DESC;
Placeholder values are substituted after insertion, so the indentation and alignment you saved survive — the output of a formatter pass, without running one.
Snippets and Surround With
Ctrl+K, Ctrl+X opens the snippet picker at the insertion point, and Ctrl+K, Ctrl+S is Surround With, which wraps the current selection in a snippet. Manage both from Tools > Code Snippets Manager (Ctrl+K, Ctrl+B), where you can register a folder of your own .snippet files. Surround With is the practical way to wrap an already-formatted SELECT in IF EXISTS (...) or inside a TRY block without re-indenting everything by hand.
8. Formatting Multiple SQL Files at Once
SSMS formats one editor window at a time. There is no format-folder command, no project-wide format action, and no Ssms.exe switch that rewrites files on disk. If you have a folder of a few hundred procedure scripts to clean up, the built-in formatter is the wrong tool.
Small batches: open everything, format per window
For a handful of files, use File > Open > File with multi-select to open them all, then work through the tabs with Ctrl+K, Ctrl+D in each and save. It is mechanical but safe, and every change ends up in one reviewable diff.
Larger batches: a command-line formatter
For a real codebase, run a formatter from a terminal so the work is repeatable and reviewable. Two options handle T-SQL:
# sqlfluff (Python): tsql dialect, fixes layout in place
sqlfluff fix --dialect tsql ./sql
# sql-formatter (Node): write to a new file, then diff
npx sql-formatter -l tsql -i 4 queries/proc.sql > queries/proc.formatted.sql
Neither one is the SSMS formatter, so the output will not match it character for character. That is usually fine, but it means you should pick one tool as the source of truth for the repository instead of alternating between Ctrl+K, Ctrl+D in SSMS and a CLI on the same files.
Make the sweep safe
- Start from a clean working tree and create a branch.
- Commit the unformatted files first, then run the formatter as a second, formatting-only commit.
- Record that commit in
.git-blame-ignore-revs and set blame.ignoreRevsFile to point at it, so blame skips the reformat.
- Read the diff before committing. Formatters disagree on T-SQL edge cases: multiple statements separated by semicolons on one line, nested string literals, unclosed quoted aliases.
After the initial sweep, format-on-save in the VS Code MSSQL extension keeps new and edited files in the same shape without anyone remembering a shortcut.
9. How T-SQL-Specific Constructs Format (CTEs, Window Functions, MERGE)
The built-in SSMS formatter knows nothing about T-SQL structure beyond indentation and keyword casing. Here is what actually happens when you press Ctrl+K, Ctrl+D on the constructs that show up constantly in production code.
CTEs and chained WITH clauses
SSMS indents each CTE body but leaves the chain flat, so a four-CTE query comes back as a wall of near-identical indentation with no separation between the CTEs. The readable form keeps the CTE name and its opening parenthesis on one line and puts the closing parenthesis flush with WITH. Expect to fix that by hand after formatting, or format the chain in the online formatter and paste it back.
Window functions
An OVER() clause with a frame specification is where formatting matters most, and it is exactly what the built-in formatter leaves alone:
SELECT
o.OrderID,
o.CustomerID,
SUM(o.OrderTotal) OVER (
PARTITION BY o.CustomerID
ORDER BY o.OrderDate
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS RunningTotal
FROM dbo.Orders o;
No automatic pass produces that layout reliably. Whichever tool you use, re-read the frame clause afterwards: ROWS BETWEEN ... AND ... is easy to misread when it is squeezed onto the same line as the ORDER BY.
MERGE and TRY...CATCH
SSMS indents the WHEN MATCHED and WHEN NOT MATCHED branches but does not align the THEN keywords or group each branch as a unit. And no formatter will add the terminating semicolon a MERGE statement requires — if yours is missing, SQL Server raises an error at run time.
What formatters will not touch: strings, JSON, XML, embedded CSS
Every SQL formatter treats string literals as opaque. That is the correct behavior, but it has consequences worth planning for:
- Dynamic SQL —
EXEC ('SELECT * FROM dbo.' + @TableName) keeps whatever the author typed. To format the inner query, format it on its own first, then wrap it and double every single quote (' becomes ''). Do not run that replacement twice over the same text.
- JSON — path expressions such as
'$.customer.name' and JSON documents stored in nvarchar use double quotes for property names, so the T-SQL parser leaves them alone. That is a feature: a formatter cannot break JSON that lives inside a string. Keep large payloads in .json files loaded with OPENJSON or OPENROWSET instead of pasting them into a query. The same rule covers XML: indentation inside FOR XML PATH('') output and XML-typed variables survives exactly as written.
- Embedded CSS or HTML — a column holding a markup fragment (style blocks with semicolons, double-quoted font names) is just a string to the formatter. The only real hazard is the single quote:
'font-family:''Segoe UI''' must keep its doubled quotes, and a formatter that re-spaces the surrounding SQL will not touch them.
Rule of thumb: if a value sits inside quotes, you own its formatting — not the tool.
10. FAQ — Format SQL in SSMS
Can SSMS format SQL automatically?
Yes. SSMS has a built-in formatter accessible via Edit > Advanced > Format Document or using the shortcut Ctrl+K, Ctrl+D to format the whole document, and Ctrl+K, Ctrl+F to format a selected block.
Does SSMS have a SQL beautifier built in?
SSMS includes a basic SQL formatter that handles indentation and keyword capitalization. For more advanced formatting — custom indentation, line breaks, spacing rules — third-party add-ins or online formatters are recommended.
What is the shortcut to format SQL in SSMS?
The default shortcut is Ctrl+K then Ctrl+D (format document) or select text and press Ctrl+K then Ctrl+F (format selection). You can customize these shortcuts under Tools > Options > Environment > Keyboard.
Can I use an online SQL formatter for T-SQL?
Yes. SQLFormat.io supports T-SQL dialect. Simply paste your T-SQL code into the editor, select T-SQL as the dialect, and format instantly — no installation needed.
Is Azure Data Studio still an option for formatting T-SQL?
No. Azure Data Studio reached end of support on February 28, 2026. Microsoft's replacement is the MSSQL extension for Visual Studio Code, which includes a T-SQL formatter: Shift+Alt+F formats the document, Ctrl+K then Ctrl+F formats the selection, and keyword or datatype casing is controlled by the mssql.format.* settings.
Can SSMS format multiple SQL files at once?
No. SSMS has no project-wide format command, and Ctrl+K then Ctrl+D only affects the active query window. To format a folder of .sql files, run a command-line formatter such as sqlfluff with the tsql dialect or the sql-formatter CLI, then review the diff before committing.