One option is to query the Dynamic Management Views. This snippet assumes that SQL hasn't been restarted, that the connection that executed is still alive, and that we want to ignore the connection executing this code..
Ideally you wouldn't use
The best way to do this is to setup a server side trace or monitor with extended events. Anything else is going to be guessing. Kev's answer is great if you meet all the requirements, but most systems will put the connection back into the connection pool as soon as the query is finished and you won't see what's needed.
You could try running a query against sys.dm_exec_query_stats and order by the last_execution_time. Assuming the query is still in cache, you'll see the most recent query, regardless of what it is, the last time it was executed.
answered May 04, 2011 at 04:30 AM
Grant Fritchey ♦♦