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 (http://www.SQLAuthority.com), BOL - sys.dm_exec_query_stats, BOL - sys.dm_exec_sql_text






is this possible in sql server 2000. Its urgent so please revert me asap. Any other way to know the same thing in sql server 2000.
[...] 4, 2008 by pinaldave Yesterday I wrote article about SQL SERVER - 2005 - Last Ran Query - Recently Ran Query. I had used CROSS APPLY in the query. I got email from one reader asking what is CROSS APPLY. In [...]
Useful query. Thanks.
Hi,
I regularly found myself trying to remember recent queries, so I wrote an add-in to store them:
http://sqlblogcasts.com/blogs/jonsayce/archive/2008/01/14/Total-SQL-Recall.aspx
Jon.
Jon,
Wonderful Add-in.
Thank you very much,
Regards,
Pinal Dave ( http://www.SQLAuthority.com )
Dave, cool script. Anyway (without running profiler constantly) we can go back (say for security reasons) and see what queries were run a day or so ago? You can email directly, too.
Thanks,
Brandon
Can we get the userID under which the SQL statements were executed?
Thanks
Sen
I was curious about this so I tried it & found the results a little difficult to believe. It shows that every fraction of a second, I’m running the create command on a stored procedure that already exists… as if I’m constantly trying to create it..but I think the SP is just executing. Thoughts?
Hi,
I am also interested in tracing out from which host address the statement got executed.
Thanks,
Vaibhav.
Hi!!
This query can be made in SQL-SERVER 2000?
Hi
I was searching so long time after i got your article.
its really wonderful.
Thanks & Regds
Palani
Hi Mate
It gives last ran queries, If an SP is ran it will give the complete syntax of the SP, But not the execution of it. e.g
CREATE PROCEDURE [dbo].[spTest]
@myParam VARCHAR(100)
BEGIN
– SOMETHING
END
it would be very nice if WE COULD HAVE something like
exec spTest @myPara = ‘abc’
I am asking more or less like a SQL Server profiler trace.
Please contact on lukegaroon@gmail.com
Nice post. But i wanted to know which alert transaction has been applied on the particular table on which date.
I tried
select *
from ::fn_dblog(null, null)
where Operation = ‘LOP_MODIFY_COLUMNS’
but it does not provide information regarding which column altered.
Can any one help me?