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://blog.SQLAuthority.com) , BOL – sys.dm_exec_query_stats, BOL – sys.dm_exec_sql_text
SQL SERVER – 2005 – Last Ran Query – Recently Ran Query
January 3, 2008 by pinaldave
Posted in Pinal Dave, SQL, SQL Authority, SQL Query, SQL Scripts, SQL Server, SQL System Table, SQL Tips and Tricks, T SQL, Technology | Tagged SQL DMV | 18 Comments
18 Responses
Leave a Reply
-
About Pinal Dave
Pinalkumar Dave is Microsoft SQL Server MVP, Solid Quality Mentor and a prominent author of over 1000 SQL Server articles at SQLAuthority. He is a dynamic and proficient Principal Database Architect, Corporate Trainer and Project Manager specializing in SQL Server Programming with over 7 years of hands-on experience. He holds a degree in Masters of Science and has accomplished a number of certifications including MCDBA and MCAD (.NET). He has also been awarded Regional Mentor for PASS Asia.
-
Blog Stats
- 8,410,859 Readers
-
SQLAuthority Links

My Homepage
My Resume
My Other Blog
--------------------
Top Downloads
PDF Downloads
Script Downloads
Script Bank
Favorite Scripts
All Scripts - 1
All Scripts - 2
Top Articles
Best Articles
Favorite Articles - 1
Favorite Articles - 2
--------------------
SQL Interview Q & A
SQL Coding Standards
SQL FAQ Download
--------------------
Jobs @ SQLAuthority
Categories
- About Me (49)
- Best Practices (82)
- Business Intelligence (6)
- Data Warehousing (27)
- Database (257)
- DBA (121)
- DigiCorp (7)
- MVP (81)
- Poll (5)
- Readers Contribution (22)
- Readers Question (28)
- Software Development (62)
- SQL Add-On (88)
- SQL Backup and Restore (48)
- SQL BOL (8)
- SQL Coding Standards (21)
- SQL Constraint and Keys (49)
- SQL Cursor (30)
- SQL Data Storage (36)
- SQL DateTime (36)
- SQL Documentation (193)
- SQL Download (198)
- SQL Error Messages (116)
- SQL Function (104)
- SQL Humor (22)
- SQL Index (82)
- SQL Interview Questions and Answers (54)
- SQL Joins (61)
- SQL Optimization (51)
- SQL Performance (204)
- SQL Puzzle (18)
- SQL Security (115)
- SQL Server DBCC (42)
- SQL Server Management Studio (17)
- SQL Stored Procedure (97)
- SQL String (17)
- SQL System Table (27)
- SQL Trigger (27)
- SQL User Group (41)
- SQL Utility (116)
- SQL White Papers (8)
- SQLAuthority (307)
- SQLAuthority Author Visit (62)
- SQLAuthority Book Review (19)
- SQLAuthority News (277)
- SQLAuthority Website Review (23)
- SQLServer (42)
- Tech (772)
- Pinal Dave (761)
- SQL Scripts (490)
- Technology (1023)
- SQL (1023)
- SQL Authority (1023)
- SQL Query (1023)
- SQL Server (1023)
- SQL Tips and Tricks (1023)
- T SQL (1023)
-
Top Posts
- SQL SERVER - Insert Data From One Table to Another Table - INSERT INTO SELECT - SELECT INTO TABLE
- SQL SERVER - Insert Multiple Records Using One Insert Statement - Use of UNION ALL
- SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server
- SQL SERVER - Retrieve Current Date Time in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}
- SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects
- SQL SERVER - Convert Text to Numbers (Integer) - CAST and CONVERT
- SQL Server Interview Questions and Answers Complete List Download
- SQL SERVER - Restore Database Backup using SQL Script (T-SQL)
- SQL SERVER - 2008 - Interview Questions and Answers Complete List Download
- SQL SERVER - 2005 List All Tables of Database
- SQL SERVER - Shrinking Truncate Log File - Log Full
- SQL SERVER - TRIM() Function - UDF TRIM()
-
Authors
-
pinaldave
- SQLAuthority News – Authors Visit – K-MUG TechEd Trivandrum on June 27, 2009
- SQLAuthority News – Book Review – Murach’s SQL Server 2008 for Developers
- SQLAuthority News – Authors Visit – DotNet Buzz Delhi TechEd Delhi on July 11, 2009
- SQL SERVER – Languages for BI – MDX, DMX, XMLA
- SQLAuthority News – FIX : Error : HP OfficeJet Scanning and Printing Gray or Pink Shades
- SQL SERVER – Disk Partition Alignment Best Practices
- SQLAuthority News – Book Review – The Rational Guide to Building Technical User Communities (Rational Guides)
- SQLAuthority News – MVP Award Renewed
- SQL SERVER – Difference between Line Feed (\n) and Carriage Return (\r) – T-SQL New Line Char
- SQL SERVER – 2008 – Policy-Based Management – Create, Evaluate and Fix Policies
-
Archives
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
Pages
Category Cloud
About Me Best Practices Database DBA MVP Pinal Dave Software Development SQL SQL Add-On SQL Authority SQLAuthority Author Visit SQLAuthority News SQL Documentation SQL Download SQL Error Messages SQL Function SQL Index SQL Interview Questions and Answers SQL Joins SQL Optimization SQL Performance SQL Query SQL Scripts SQL Security SQL Server SQL Stored Procedure SQL Tips and Tricks SQL Utility Technology T SQL



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?
Can anyone please tell me how to do this in sql 2000. Having problems with “Cross Apply”
Great code! For anyone trying to use it make sure it run it against the MASTER database.
BUT, Is there anyway I can get it to go back farther? It seems to only display records since the last time I booted.
How to rollback last recently ran query in SQL Server. If we had not mentioned rollback to Savepoint or begin Trasaction methods. Is there any alternative.
@Ankur,
Point in Time Recovery ( If you have your database in Full Recovery Model)
~ IM.
Thanks for response