How can I get the queries history in SQL Server?

5 pts.
Tags:
SQL Server
Is there a way I can get the queries history in SQL Server?

Software/Hardware used:
how can i get the queries history in sql server

Answer Wiki

Thanks. We'll let you know when a new response is added.

Hope this help :

SELECT deqs.last_execution_time AS [Time], dest.TEXT AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.last_execution_time DESC

Discuss This Question: 4  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Elizine
    If SQL Server hasn't been restarted (and the plan hasn't been evicted, etc.), you may be able to find the query in the plan cache.
    SELECT t.[text][/text]
    FROM sys.dm_exec_cached_plans AS p
    CROSS APPLY sys.dm_exec_sql_text(p.plan_handle) AS t
    WHERE t.[text][/text] LIKE N'%something unique about your query%';
    1,130 pointsBadges:
    report
  • Subhendu Sen
    You might be retrieved info from cached query plans or use external tool like ApexSql.
    86,760 pointsBadges:
    report
  • danieljones

    If SQL Server hasn't been restarted, you may be able to find the query in the plan cache.

    SELECT t.[text][/text]

    FROM sys.dm_exec_cached_plans AS p

    CROSS APPLY sys.dm_exec_sql_text(p.plan_handle) AS t

    WHERE t.[text][/text] LIKE N'%something unique about your query%';

    If you lost the file because Management Studio crashed, you might be able to find recovery files here:

    C:\Users\<you>\Documents\SQL Server Management Studio\Backup Files\

    2,825 pointsBadges:
    report
  • edwinjhoffer
    you can use
    DBCC FREEPROCCACHE WITH NO_INFOMSGS;
    NOTE: use only if you not using production enviornment
    3,495 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: