Welcome to MSSQL Forum!
MSSQL Forum - Discussions about Microsoft SQL Server

You are currently viewing our community forums as a guest user. Sign up or
Having an account grants you additional privileges, such as creating and participating in discussions.

Recovering an accidentally closed query (provided you ran it)

Discussion in 'SQL Server Scripts and Tools' started by Peter Schmitz, Jan 27, 2017.

  1. Peter Schmitz

    Peter Schmitz Administrator Staff Member

    The other day I was slightly over-enthusiastic when closing tabs, and I ended up closing the tab I had been working in. Several hours of work lost, apparently, as because I had not saved my work, SSMS did not create an auto-save file for me.

    Luckily, though, I had been executing the query as I was developing it, and that proved to be my rescue. As it turns out, SQL Server keeps track of queries that got executed, and you can pull the info from the system tables using the following query:

    Code (sql):
    USE <database>
        execquery.last_execution_time AS [DATE TIME]
        , execsql.text AS [Script]
        sys.dm_exec_query_stats AS execquery
        sys.dm_exec_sql_text(execquery.sql_handle) AS execsql
        execquery.last_execution_time DESC

Share This Page