Get Last Executed SQL Command (T-SQL)

One of my developers working with a trigger logging tool in SQL Server 2008 asked me if there was a command to get the last SQL command executed in T-SQL. I thought there was a system stored procedure for such a function, but it’s possible that I’m thinking of another product over the past decade ... online searches have not given us any results.

Does anyone have information on something like this?

+5
source share
3 answers

be sure to try the following:

SELECT
DMExQryStats.last_execution_time AS [Executed At],
DMExSQLTxt.text AS [Query]
FROM
sys.dm_exec_query_stats AS DMExQryStats
CROSS APPLY
sys.dm_exec_sql_text(DMExQryStats.sql_handle) AS DMExSQLTxt
ORDER BY
DMExQryStats.last_execution_time DESC

it will return recently executed requests along with the date and time of their execution

+18

, , SQL, :)

sys.dm_exec_query_stats sys.dm_exec_procedure_stats, , , last_execution_time. , , , .

+4

What does β€œlast” mean in the context of a multi-core machine?

In addition, does he mean the last launched or the last completed?

Finally, he should just open SSMS and look at Activity Monitor.

+1
source

All Articles