View recently run queries in MSSQL 2005

Categories: SQL

Pinal Dave posted this great little SQL snippet to view most recent queries executed in 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

This is definitely a useful SQL snippet to see what's just happened when you're in a pinch.

Comments

Bobby Nichols's Gravatar Er. Looked cool. Thought i could use it. Tried it. Didn't have the access level on the SQL Server that I needed. :(

Perhaps its because its been a while since i have developed in a MS SQL environment (used to Oracle), but some more explanation about *what* the query is doing would help me understand how MS SQL Server works.

Still i copied the snippet into my SQL notebook. :)
Dan G. Switzer, II's Gravatar @Bobby:

You should only run this on a database you have access to. I'm not exactly sure what level of access you need in order to sys tables & stored procs. You might want to head over to Pinal Dave's post and ask the question there.

Add Comment



If you subscribe, any new posts to this thread will be sent to your email address.