Skip to content

Repository files navigation

SSMS SQL Formatter (Rules + AI)

⬇ Download the latest release — free, MIT-licensed.

Quick help

Format with Ctrl+Shift+Alt+F, right-click → Format T-SQL Script, or the Tools menu. Formats the selection if there is one, otherwise the whole document; Ctrl+Z undoes the entire format. Settings live under Tools → Options → Format T-SQL Script (General, AI Engine, and Help pages). Scripts with syntax errors are never modified, and comments are never silently deleted. A Format T-SQL Script Help entry in the Tools menu shows this summary inside SSMS.

Format on save: turn on Format on save (General page) to auto-format every .sql document right before it's written to disk (Ctrl+S, Save All, etc.). This always uses the rule-based engine — never AI — so saving is never delayed by a network call or a confirmation prompt, and a script that fails to parse is saved untouched.

Format on paste (new, please report any issue): turn on Format on paste (General page) to auto-format a .sql document right after pasting multi-line text into it. Detected as a single edit that inserts multi-line text, so ordinary typing is never affected. Same rule-based-only, parse-failures-left-untouched behavior as format on save.

Format Files...: Tools menu → pick one or more .sql files and format them on disk in place (rule-based engine, original file encoding preserved). Useful for formatting a batch of migration/deployment scripts at once. A file that fails to parse is left untouched and reported at the end.

Format All Open Files: Tools menu → formats every currently-open .sql document in place. Each is a normal editor edit (Ctrl+Z in that window undoes it) — nothing is written to disk unless you save afterward. A document that fails to parse is left untouched.

Copy results to Excel: click in the results grid, select cells (Ctrl+A for all), press Ctrl+Shift+Alt+X, then Ctrl+V in Excel — you get a real table with bold headers and (by default) all cells as Text so leading zeros and long IDs survive. Configure under Tools → Options → Format T-SQL Script.

Export options: choose .xlsx or .csv, set an output folder (or be prompted per export), and optionally include a Query sheet recording the SQL that produced the data. Workbooks get a frozen header row and AutoFilter dropdowns by default. Share your whole configuration with Tools → Export / Import Formatter Settings.

Shared team config: drop a .sqlformatter.json (same format as Export Formatter Settings) into a repo folder, and every format of a file under that folder — or a descendant folder — picks it up automatically, both in SSMS and from the CLI below. It overrides just that one operation; your own Tools > Options settings are never modified. Turn this off with Use folder-level .sqlformatter.json (General page) if you don't want a folder's contents to affect formatting.

CI / command-line use

tools/SsmsSqlFormatter.Cli builds a standalone ssmssqlfmt.exe (rule-based engine only — no AI, no SSMS/VS install required) for gating a build or PR on "SQL is formatted":

ssmssqlfmt check <file-or-directory...> [--config settings.json]   # reports what would change, exits 1 if anything would
ssmssqlfmt format <file-or-directory...> [--config settings.json]  # formats in place, same as "Format Files..." in the VSIX

A directory argument is searched recursively for *.sql files. --config takes the same JSON file produced by Tools → Export Formatter Settings inside SSMS, so a team can share one style file between the IDE and CI. If --config is omitted, each file's own directory (and its ancestors) is searched for a .sqlformatter.json, same as the VSIX's shared-config discovery above. Example GitHub Actions step:

- name: Check SQL formatting
  run: tools\SsmsSqlFormatter.Cli\bin\Release\net48\ssmssqlfmt.exe check sql\ --config .sqlformatter.json

Results grid: SSMS's results grid builds its own private context menu, so right-clicking the grid won't show the extension's commands. Use Ctrl+Shift+Alt+X, the Tools menu, or add a toolbar button (Tools → Customize → Commands → Toolbar → Standard → Add Command → Tools → Copy Results as Excel Table) — a toolbar button is always visible and survives upgrades.

Note: on some SSMS builds the right-click entry doesn't appear in the query editor's context menu automatically, because SSMS's T-SQL editor uses its own private context menu rather than the standard Visual Studio one. The keyboard shortcut and Tools menu always work. To add the context-menu entry yourself (one-time, survives upgrades): Tools → Customize → Commands tab → select Context menu → "Editor Context Menus | Code Window" → Add Command → category Tools → Format T-SQL Script → OK → Close. Alternatively add it to a toolbar the same way via the Toolbar radio button.

A VSIX extension for SQL Server Management Studio 21 / 22 that formats T-SQL in the query window.

Two engines:

Engine How it works Pros Cons
Rule-based Microsoft ScriptDom parser + script generator (fully offline) Instant, deterministic, understands all T-SQL Regenerating from the parse tree can drop/move comments
AI Anthropic Claude Messages API, or GitHub Copilot / an OpenAI-compatible chat endpoint — pick under AI Engine → Provider, with your own API key or token Preserves comments, follows free-form style instructions ("leading commas", "align equals signs") Needs network + API key/token; script text is sent to the API

Two built-in style presets — Classic (old-format compact: trailing commas, inline JOINs, fewer line breaks) and Modern (each column/JOIN/predicate on its own line, aligned bodies) — plus a Custom preset where every individual option applies.

Important caveat

Microsoft does not officially support extensions in SSMS 21/22. They are tolerated and widely used (SSMSBoost, community formatters, etc.), but if SSMS misbehaves with an extension installed, Microsoft won't help. Use at your own risk, and keep the VSIX handy so you can uninstall via the VSIX installer if needed.

Prerequisites (build machine)

  • Windows with Visual Studio 2022 (Community is fine)
  • Workload: Visual Studio extension development (installs the VS SDK)
  • .NET Framework 4.8 targeting pack (included with the workload)
  • SSMS 21 or 22 installed (to install/test the result)

Build — no Visual Studio needed (GitHub Actions)

Don't want to install VS? Push this folder to a GitHub repo (public or private). The included workflow (.github/workflows/build.yml) compiles it on GitHub's free Windows runners:

  1. Create a repo and push this project (git init, git add ., git commit, git push).
  2. On GitHub, open the Actions tab → the "Build VSIX" run → download the SsmsSqlFormatter-vsix artifact. That zip contains the ready-to-install SsmsSqlFormatter.vsix.
  3. Tag a version (git tag v1.0.0 && git push --tags) to get the VSIX attached to a GitHub Release — a permanent free download link you can share.

Build — locally

Run build.cmd from the project root (needs VS 2022 with the extension development workload), or manually:

  1. Open SsmsSqlFormatter.sln in Visual Studio 2022.
  2. Restore NuGet packages (right-click solution → Restore NuGet Packages). If the pinned Microsoft.SqlServer.TransactSql.ScriptDom version isn't found, update it to the latest 161.x via NuGet Package Manager.
  3. Switch to Release and Build. The output is: src\SsmsSqlFormatter\bin\Release\SsmsSqlFormatter.vsix

Install into SSMS

SSMS doesn't (yet) have an Extensions marketplace, so install manually:

Option A — double-click the .vsix. On SSMS 21+, the VSIX installer usually detects SSMS as a target ("SQL Server Management Studio 21"). Tick it and install.

Option B — command line (if the installer doesn't list SSMS):

"C:\Program Files\Microsoft SQL Server Management Studio 21\Release\Common7\IDE\VSIXInstaller.exe" SsmsSqlFormatter.vsix

(Adjust the path for your SSMS install; SSMS 22 lives under ...Management Studio 22\....)

Restart SSMS after installing.

Uninstall: VSIXInstaller.exe /uninstall:SsmsSqlFormatter.7f3d2a1e-5b8c-4e9f-a6d0-1c4b7e2f9a35

Use

  • Open a query window, press Ctrl+Shift+Alt+F, or
  • Right-click in the editor → Format T-SQL Script, or
  • Tools → Format T-SQL Script

With text selected, only the selection is formatted; otherwise the whole document. The change is one undo unit — Ctrl+Z reverts it.

Configure

Tools → Options → SQL Formatter

  • General: engine (RuleBased / Ai), style preset (Classic / Modern / Custom), keyword casing, indent size, semicolons, per-clause line breaks, multiline lists, alignment, comment warning.
  • AI Engine: provider (Anthropic / Copilot), API key or token, model, endpoint, max tokens, timeout, custom style instructions, "use General options as style guide", fallback to rule-based on error, confirm-before-send.

AI engine notes

  • Anthropic: get a key at https://console.anthropic.com — API usage is billed to that key separately from any Claude.ai subscription.
  • Copilot: point Endpoint at your Copilot-compatible chat completions endpoint and use a bearer token as the key. Any OpenAI-compatible chat endpoint (choices[].message.content response shape) works the same way.
  • The key/token is stored in Windows Credential Manager for the current user, not in plain text.
  • Your script text — including any embedded literals/data — is sent to the API. The confirm-before-send prompt is on by default for that reason.
  • For very large scripts, raise Max output tokens and Timeout.

Debugging

F5 launches the Visual Studio experimental instance (the manifest also targets VS 2022 so the extension loads there). To debug inside SSMS itself, change StartProgram in the .csproj to your Ssms.exe path, or attach the debugger to a running Ssms.exe process.

Known limitations

  • Rule-based engine: regenerates the script from the AST, so comments can be dropped (you get a warning when comments are detected — or use the AI engine, which preserves them).
  • Rule-based engine leaves the script untouched if it doesn't parse (you'll get the parse error with line/column — arguably a feature).
  • SQLCMD-mode scripts (:setvar, :connect) won't parse with ScriptDom; use the AI engine for those.
  • After a new major SSMS release, the manifest's InstallationTarget upper bound ([21.0,23.0)) may need bumping.

Project layout

src/SsmsSqlFormatter/
  SsmsSqlFormatterPackage.cs   – AsyncPackage, registers command + options pages
  FormatSqlCommand.cs          – command handler (selection/document, undo unit, status bar)
  SsmsSqlFormatter.vsct        – menus + Ctrl+Shift+Alt+F keybinding
  source.extension.vsixmanifest – targets Microsoft.VisualStudio.Ssms [21.0,23.0)
  Formatting/
    ScriptDomFormatter.cs      – rule-based engine + Classic/Modern/Custom presets
    AiFormatter.cs             – Anthropic Messages API / Copilot chat API client
  Options/
    GeneralOptions.cs          – Tools>Options "General" page
    AiOptions.cs               – Tools>Options "AI Engine" page

About

T-SQL formatter for SSMS 21/22 - rule-based + AI engines, comment preservation, fully customizable

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages