SQL SERVER – Recently Executed T-SQL Query

About a year ago, I wrote blog post about SQL SERVER – 2005 – Last Ran Query – Recently Ran Query.  Since, then I have received many question regarding how this is better than fn_get_sql() or DBCC INPUTBUFFER.

The Short Answer in is both of them will be deprecated.

Please refer to following update query to recently executed T-SQL query on database.

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)

SQL DMV, SQL Function, SQL Scripts, SQL System Table
Previous Post
SQLAuthority News – First Editorial – T-SQL Challenges Beginners
Next Post
SQLAuthority News – Happy Diwali and New Year

Related Posts

14 Comments. Leave new

  • Hi Pinal,

    I ran this query it resulted with so many records.bit confusing. can you please let me know whats this query do?


    Sanjeev Kumar

  • hi,
    i tried above query i am getting output but i am not understanding output .

    plz let me know in the query deqs means …..

    Advance Thanks,
    Rama krishna

  • hi ,
    i have one dought about exception handling in sql server 2005,
    i.e, if any error find in try block , is error display in front end or not

  • Harshavardhan
    March 24, 2011 2:23 pm

    Hi Dave,

    i wanted the recently executed query with user details with it. Is it possible that i can get these details.
    please help me out.

  • Mahendra Kumar Meena
    January 24, 2013 2:05 pm

    Could we get the username also, who has executed what?

  • Hi Pinal,

    How to give specific access permission to different windows account to access to SQL Server. Suppose i have ‘india-2348test’ windows a/c, i am going to login with test windows a/c. But whatever permission i have given in administrator a/c i.e. user can not insert data into the student table, but he can select(read) the student table. this permission is for ‘india-2348test’ a/c. so, no one can insert record in student table, but can read data from student table. plz help me out…
    Thanks in advanced.

  • how to get last executed query by user in sql server?
    not server in-build stored procedure!

  • how to get list of queries executed on sql server by respective GUI application

  • Hi Pinal,

    When a search expression is being run in GUI application, how to trace the corresponding query on SQL Server.

    Thanks in advance.

  • Thank you so much for this query.

  • I’m getting an error message when I attempt to run this query:

    Msg 102, Level 15, State 1, Line 2
    Incorrect syntax near ‘.’.

    Not sure how to modify the script to fix the error, or exactly why its returning the error. Copied and pasted exactly as it was posted in the blog.

  • Hi How to find how many insert,update and deleted query ran last 24hrs. please reply the commaned

  • I am getting the same exact problem

  • What if I don’t have access?

    Mens. 300, Nivel 14, Estado 1, Línea 1
    VIEW SERVER STATE permission was denied on object ‘server’, database ‘master’.
    Mens. 297, Nivel 16, Estado 1, Línea 1
    The user does not have permission to perform this action.


Leave a Reply