You can only retrieve an unsaved query in SQL Server Management Studio if something already preserved it outside your normal "Save" habit—most reliably through Query History data, an AutoRecover backup taken during a registered-server session, or a copy of the .sql file that SSMS created before the unsaved edit. The key condition: SSMS writes no recovery copy for a brand-new, never-saved query in a routine session, and it does not keep an unlimited undo log after the window closes. So the realistic first move is not to hunt for a magic "recover unsaved" button. It is to stop working in that window, check the three places SSMS may have left a trace, and then fall back to system-level recovery for the deleted or overwritten file.

This article lays out what SSMS actually preserves, how to check each location in the correct order, how to attempt recovery when the window is already gone, and where the hard limits sit. The behavior described matches SQL Server Management Studio 18.x and 19.x; 20.x moved to a 64-bit Visual Studio shell and altered several surrounding features, so verify version-specific menu paths before relying on them.

First, understand what "unsaved" means in SSMS

SSMS does not maintain a scratch session store the way some editors do. Whether a recovery path exists depends on how the query arrived in the editor:

Query state What SSMS typically holds Recovery chance after a crash or close
New query, never saved, no execution Only the in-memory document Very low unless AutoRecover was active
Saved once, then edited The on-disk .sql file plus your unsaved edits in memory Good if AutoRecover ran; otherwise the file holds the last save
Saved to a file, file later deleted Nothing inside SSMS Depends on filesystem or version-control recovery
Executed at least once against an instance Optionally a Query History entry, if configured Good while the history dataset exists

The distinction matters because people often say "unsaved" when they mean "I saved it but not my last two hours of changes." Those are different problems with different recovery routes, and mixing them up sends you looking in the wrong folder.

Prerequisites before you start digging

Three things determine whether any recovery is possible, so establish them first.

Confirm the current state of the window. If the SSMS window is still open, even if the tab seems blank or the query appears lost, do not close it. Modern SSMS autosaves editor state for open tabs at shutdown or crash and can restore unsaved tabs the next time you reopen the application for the same connection context. A restart, not a forensic file hunt, is sometimes the whole answer.

Identify your SSMS version. Check Help > About in the SSMS menu. Version 18.x and 19.x behave similarly for AutoRecover and Query History. Version 20.x changed the shell and some settings locations, so path names below may differ slightly.

Know where your user profile stores application data. SSMS keeps its AutoRecover and settings data under your Windows user profile, typically in folders under %APPDATA%\Microsoft\SQL Server Management Studio\ and %USERPROFILE%\Documents\Visual Studio\ style locations, depending on version. You do not need to memorize the exact string; you need to be able to browse there, so open File Explorer and paste the environment variable paths rather than typing them from memory.

Check order: fastest reversible steps first

Work through these in sequence and stop as soon as you find the text. Each step is reversible and none modifies your database.

Step 1 — Do not close the window; try restart recovery. If SSMS crashed or you closed it accidentally, reopen it and watch for a document-recovery prompt or a restored tab. Success looks like your query text reappearing in a tab, sometimes marked as recovered or unsaved. Check the text before you edit anything, then save it to a real file immediately.

Step 2 — Look for AutoRecover files. SSMS periodically writes recovery copies of modified documents when AutoRecover is enabled (it is on by default in the versions covered here, but confirm under Tools > Options > Environment > AutoRecover). Browse to the AutoRecover folder in your user profile and look for files with names matching your query or a generated recovery name and recent timestamps. Open candidate files with a plain text editor or SSMS itself. A successful recovery shows the query body with the most recent autosave timestamp; a failure is an empty or stub file.

Step 3 — Search the whole profile for recently modified .sql copies. If you do not know the AutoRecover path for your build, search File Explorer for files modified in the last day with .sql or backup-style extensions under your user profile. Sort by date modified. This catches recovery copies and stray "save as" outputs you forgot about.

Step 4 — Check Query History. If Query History was enabled (Tools > Options > SQL Server Object Explorer or the Query Execution settings, depending on version) and the query ran at least once while connected through a registered server, the executed text may be stored. Open the Query History view and filter by date and server. If your query never executed successfully, this will be empty—that is a normal result, not a sign you did something wrong.

Step 5 — Check version control and synced folders. If the file was saved earlier and lived in a Git working tree, OneDrive, or another synced directory, look at the prior version, the sync client's version history, or the recycle bin. This recovers the last saved state, not unsaved edits.

Step 6 — Fall back to filesystem recovery only for deleted files. If the query existed as a .sql file that has been deleted, check the Recycle Bin first, then a File History or backup snapshot. Deeper undelete tools are a last resort and should be run only after you stop writing to that drive; they recover files, not in-memory text.

The limits you need to accept

SSMS is a query editor, not a document-recovery system. A brand-new query you typed, never saved, and never executed in a session without AutoRecover activity can be gone when the process ends, and no supported SSMS feature will bring it back. AutoRecover interval is configurable, so up to one interval of typing can be missing even on a successful recovery. Recovery copies are also tied to the document's identity: renaming, closing without saving, or opening a different file in the same tab can orphan them. Query History, where available, captures executed statements, not your editing drafts before execution.

That is why the durable answer is procedural: save early to a real path, keep query files in a synced or version-controlled folder, and enable AutoRecover with a short interval if you routinely write long ad hoc scripts.

What to do differently so this does not happen again

Save a new query to a file before you write more than a few lines, even a throwaway one. Keep ad hoc scripts in a synced folder such as OneDrive or a Git repo so prior versions exist. Confirm AutoRecover is on and set its interval shorter than your typical work burst. Enable Query History if you often need to recall what you ran, not just what you saved. Treat every query tab as unsaved until you verify a file path exists.

FAQ

Does SSMS have an "undo close tab" like a browser? Not as a reliable, version-stable command. The practical equivalent is restart recovery for open sessions and AutoRecover files for modified documents.

Will the query still be recoverable after I shut down the computer? Only if an AutoRecover copy, saved file, or query-history entry was written before shutdown. In-memory text does not survive a clean shutdown, and SSMS offers no post-shutdown draft store.

Can I recover a query that ran but was never saved? Check Query History if it was enabled and the statement executed against a registered server. If history was off, the executed text was not retained by SSMS.

Is there a risk in opening recovery files from a crashed session? Low for text files, but verify content before executing anything, and never run recovered scripts against production without reviewing them.