Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recover unsaved SQL query scripts

How to recover the unsaved scripts if the SSMS crashes / unsaved tab gets accidentally closed?

like image 937
BumbleBee Avatar asked Sep 28 '22 17:09

BumbleBee


People also ask

How do I reopen a closed SQL query?

All you have to do is reopen SSMS and you're presented with the 'Microsoft SQL Server Management Studio Recovered Files' window. Simply clicking 'Recover Select Files' will open a temporarily saved copy of the Query window.

Where are SQL scripts saved?

The default location for files and projects is the SQL Server Management Studio Projects folder in your My Documents folder.

Where are temp SQL files stored?

The location of the temporary file is specified by the TMP, TMPDIR, or TEMP environment variable or by the -dt database server option. For example, dbeng16 -dt "." creates a temporary file in the current directory. The temporary file's location can be a performance consideration.


1 Answers

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
like image 472
BumbleBee Avatar answered Oct 18 '22 03:10

BumbleBee