SQL SSMS Recover Closed Tabs

While using SQL Management Studio you may encounter a scenario whereby an unsaved tab was closed or SSMS itself was exited while it contained unsaved tab. It is possible to recover the scripts you were work on, though time is critical.

Option 1. If SSMS was not actually closed, the tabs still exist as "AutoRecovery" backup files. These are located under a Visual Studio folder.. C:\Users\{username}\Documents\Visual Studio 2015\BackUp Files\Solution1.

These are .sql files that you can simply double-click to reopen within SSMS. Alternatively you can rename these files and move to some other folded than this one. This folder does get emptied upon SSMS being closed.


Option 2.
SQL Server keeps a log of every script processed for the last 24 hours. This is not limited to scripts ran from SSMS, but EVERY script SQL has processed (even from Acclamare).

Run the following script and then copy/paste from the result's script column.

USE [database]
SELECT execquery.last_execution_time AS [Date Time], execsql.text AS [Script]
FROM sys.dm_exec_query_stats AS execquery
CROSS APPLY sys.dm_exec_sql_text(execquery.sql_handle) AS execsql
WHERE execsql.text LIKE '%{some text}%'
ORDER BY execquery.last_execution_time DESC

*** Use the WHERE command along with inserting {some test} to narrow your results if you can remember some text from your script.

 I found this article helpful. (1)