SQL SERVER – 2005 – Last Ran Query – Recently Ran Query

How many times we have wondered what were the last few queries ran on SQL Server? Following quick script demonstrates last ran query along with the time it was executed on SQL Server 2005.

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

Reference : Pinal Dave (https://blog.sqlauthority.com) , BOL – sys.dm_exec_query_stats, BOL – sys.dm_exec_sql_text

, ,
Previous Post
SQLAuthority New – Best Practices for Speeding Up Your Web Site
Next Post
SQL SERVER – Quick Note on CROSS APPLY

Related Posts

65 Comments. Leave new

  • This query was a life saver thanks

    Reply
  • Annada Prasad Nanda
    February 18, 2012 3:14 pm

    I want all records after today.what is the particular query

    Reply
  • Rajesh bhimradia
    April 4, 2012 6:39 pm

    hello…
    I have executed a query yesterday, but i don’t know which query i have ran. So, can you tell me where could i got the ran query log in sql server 2008.

    Reply
  • Freaking genious buddy.
    You sir saved my bacon :)

    Reply
  • Mark Anthony
    July 5, 2012 12:59 pm

    Good day, sir.

    I have created a query that makes use of CROSS APPLY. I was able to successfully create it. My problem is that when I tried to integrate it to Delphi dataset, it’s giving me an error when I try to activate the dataset. It seems that Delphi doesn’t want CROSS APPLY because when I try to remove the CROSS APPLY from my SQL code and run it again, it does not give any error at all. By the way, I’m using Delphi 7.

    I hope to read enlightenment. Thank you.

    Reply
  • Pinal, Thanks for this query.

    Is it possible to get more than one day’s data for the same. By this query I am getting today’s data. Old executed queries are not coming in the recordset. Is there any way to get it?

    Thanks,
    Shaiju CK

    Reply
  • SELECT *
    FROM SystemConfig_Ctl AS D
    CROSS APPLY dbo.Split(D.SysValue,’,’) AS ST

    What goes wring in this query

    Thanks
    Labinash

    Reply
  • In SQL Server how to undo last executed query ?

    Reply
  • What if our SQL server gets rebooted?

    Reply
  • Rohit Bhagat
    May 21, 2013 10:46 am

    DECLARE @EndTime DATETIME
    DECLARE @StartTime DATETIME
    SET @StartTime = GETDATE()
    Select * From Test
    SET @EndTime = GETDATE()

    PRINT ‘StartTime = ‘ + CONVERT(VARCHAR(30),@StartTime,121)
    PRINT ‘ EndTime = ‘ + CONVERT(VARCHAR(30),@EndTime,121)
    PRINT ‘ Duration = ‘ + CONVERT(VARCHAR(30),@EndTime,114) + ‘(hh:mi:ss:mmm)’

    Reply
  • Dharmesh Rajodiya
    April 11, 2014 5:24 pm

    Can i get the log of all the queries fired from SSMS?

    Reply
  • Sir,
    I want to know the last ran query for a particular database and when I use DBID, the query returns null.

    Reply
  • Hi Dave, My Cross Apply takes more than a minute between two tables, one with 50 K records and other with 150 records. Please help to tune the query. Thanks in Advance!

    Reply
  • as pueder see the changes implemented by my date and bestowed any SQL database

    Reply
  • I thought i have lost my query after someone close my session, thank you so much! :D

    Reply
  • Rajendraprasad
    July 7, 2017 12:19 am

    I have rebooted my server today, now i want to see my query execution history for last two days on my server.

    this query helpful:

    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

    If not please provide solution for this

    Reply
  • José Adrián Mendoza García
    August 7, 2017 11:20 pm

    The best to audit the DB. Thank you!

    Reply

Leave a Reply

Menu