How to recover the unsaved scripts if the SSMS crashes / unsaved tab gets accidentally closed?
Posting this in case if somebody stumbles into same problem.
Googled for Retrieve unsaved Scripts and found a solution.
Run the following select
script. It provides a list of scripts and its time of execution in the last 24 hours. This will be helpful to retrieve the scripts, if we close our query window in SQL Server management studio without saving the script. It works for all executed scripts not only a view or procedure.
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
ORDER BY execquery.last_execution_time DESC
I was able to recover my files from the following location:
C:\Users\<yourusername>\Documents\SQL Server Management Studio\Backup Files\Solution1
There should be different recovery files per tab. I'd say look for the files for the date you lost them.
I use the free file searching program Everything, search for *.sql files across my C: drive, and then sort by Last Modified, and then browse by the date I think it was probably last executed.
It usually brings up loads of autorecovery files from a variety of locations. And you don't have to worry where the latest version of SSMS/VS is saving the backup files this version.
I am using Windows 8 and found the missing scripts in the path below:
C:\Users\YourUsername\Documents\SQL Server Management Studio\Backup Files
SSMSBoost add-in (currently free)
+tons of other features. (I am the developer of the add-in)
You can find files here, when you closed SSMS window accidentally
C:\Windows\System32\SQL Server Management Studio\Backup Files\Solution1