Monday, July 14, 2014

Retrieve unsaved SQL query Scripts

So, the worst has happened and your elaborate work was lost because you didn't press save and then your session was unexpectedly closed? or SSMS crashed and you cannot restore the script?

Well, there is a solution to retrieve unsaved SQL Scripts, if you have executed the script within the last 24 hours:

http://www.sql-programmers.com/retrieve-unsaved-sql-query-scripts.aspx

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