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
65 Comments. Leave new
I want all records after today.what is the particular query
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.
Freaking genious buddy.
You sir saved my bacon :)
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.
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
SELECT *
FROM SystemConfig_Ctl AS D
CROSS APPLY dbo.Split(D.SysValue,’,’) AS ST
What goes wring in this query
Thanks
Labinash
In SQL Server how to undo last executed query ?
If you used transaction, use rollback
What if our SQL server gets rebooted?
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)’
Can i get the log of all the queries fired from SSMS?
Sir,
I want to know the last ran query for a particular database and when I use DBID, the query returns null.
DBID and ObjectID columns are populated only for stored procedure not adhoc queries.
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!
as pueder see the changes implemented by my date and bestowed any SQL database
I thought i have lost my query after someone close my session, thank you so much! :D
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
The best to audit the DB. Thank you!