HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlCritical

Can I see Historical Queries run on a SQL Server database?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
cansqlseedatabasehistoricalserverqueriesrun

Problem

Someone was running a query on our SQL Server database remotely and their system crashed.

They have no backup of that query and want to see what was run on the server.

Is it possible to find this query in a log or in a history somewhere?

Solution

Similar Grant Fritchey had the issue where he had closed SSMS and lost the query he had been working on...blogged about here:
Oh !

EDIT

To make this a bit more detail of an answer, the referenced linked above Grant provides a query to simply go to the cache on the instance to pull out the query you had just executed (or atleast attempt to):

SELECT  dest.text
FROM    sys.dm_exec_query_stats AS deqs
        CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
WHERE   deqs.last_execution_time > '5/19/2011 11:00'
        AND dest.text LIKE 'WITH%';


A few more options that were noted in the comments of Grant's blog:

  • Jamie Thomson points where SSMS actually has a "recovery" area under your Windows profile, similar to Word or Excel recovery.



  • Another individual notes in comments about the SSMS Tools Pack, but this add-on is only free for SQL Server 2008. Starting with SQL Server 2012 it is paid only, but has many features you might find useful.

Code Snippets

SELECT  dest.text
FROM    sys.dm_exec_query_stats AS deqs
        CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
WHERE   deqs.last_execution_time > '5/19/2011 11:00'
        AND dest.text LIKE 'WITH%';

Context

StackExchange Database Administrators Q#4043, answer score: 54

Revisions (0)

No revisions yet.