Quick Fix Summary
TL;DR: In SSMS 2026, press Ctrl+Shift+R to open the “Recover Unsaved Files” pane. If that’s empty, check %USERPROFILE%\AppData\Local\Temp for .sql files. If you’re using SQL Developer, press F8 and look under the History tab. If those fail, restore from your latest database backup or ask your DBA to search the plan cache.
What’s causing this mess?
When SSMS or SQL Developer crashes mid-session, your work doesn’t just disappear—SSMS saves temporary .sql files in your local Temp folder, while SQL Developer keeps a short-lived history list. The trick is grabbing that text before Windows or the app cleans it up (usually within 7–30 days, depending on your settings).
Here’s exactly how to get it back
For SQL Server Management Studio (SSMS) 2026:
- Fire up SSMS again. Hit File → Recover Unsaved Files or mash Ctrl+Shift+R.
- See a file list? Double-click the one you want, then hit File → Save As immediately—otherwise SSMS might delete the temp file next time you open it.
- No files showing up? Open File Explorer, paste %USERPROFILE%\AppData\Local\Temp into the address bar, and sort by “Date modified” (newest first). Look for files starting with ~vs or ending in .sql.
- File looks wonky? Run SSMS as Administrator, then go to Tools → Options → Environment → AutoRecover. Bump “Keep AutoRecover information for” up to 30 days and restart SSMS.
For Oracle SQL Developer 22.2.1-as-of-2026:
- Launch SQL Developer. Hit F8 or go to View → History.
- In the History pane, click the SQL tab. You’ll see recent commands with timestamps.
- Double-click any entry to reopen it in the editor, then smash Ctrl+S to save it to disk.
When the usual tricks don’t cut it
- Plan cache search (SSMS only): Run this as sysadmin:
SELECT TOP 10 qs.last_execution_time, qt.text FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt ORDER BY qs.last_execution_time DESC;
Copy the qt.text column into a fresh query window and save it. - Database backup restore: If the query ran on a production server, ask your DBA to restore the latest msdb backup or enable Query Store (SSMS 2016+) to replay it.
- Third-party tools: Apps like ApexSQL Recover or Redgate SQL Toolbelt can dig through transaction logs for lost batches—just expect to pay $200–$400.
Stop this from happening again
- In SSMS: Tools → Options → Environment → AutoRecover; set “Keep AutoRecover information for” to 30 days and turn on “Save auto-recover information every 2 minutes.”
- In SQL Developer: Tools → Preferences → Database → Worksheet; enable “Save history for” and set it to 90 entries.
- Always close query windows with Ctrl+F4 instead of the X button—SSMS treats that as “Save changes” and writes the file to disk right away.
- For teams, enable Query Store on the database (compatibility level 130+). It keeps a rolling 24-hour history of executed queries, even after the session ends.
Sources: Microsoft Learn – SSMS AutoRecover, Oracle SQL Developer Docs, Redgate SQL Toolbelt
