Open closed document in MS SQL SERVER



We can retrieve the query even we closed our query window in SQL without saving the script.

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

Above query provides a list of scripts and its time of execution in the last 24 hours. 

This will be work for all executed scripts not only a view or procedure.