SQL command history from MSSQL Server
April 25, 2014
Working on a project recently where I had no access to the original code which generates some sales reports for a customer. After a little digging I found this gem of a chunk of SQL which shows you all recent SQL queries to the MSSQL server.
SELECT SQLTEXT.text, STATS.last_execution_time
FROM sys.dm_exec_query_stats STATS
CROSS APPLY sys.dm_exec_sql_text(STATS.sql_handle) AS SQLTEXT
WHERE STATS.last_execution_time > GETDATE()-1
ORDER BY STATS.last_execution_time DESC
I was able to see what queries they used to generate their report and then replicate them in my own code to make that data available to my systems.