MS SQL Server query log

The Problem

I need to find the sql query another program was running on the MS SQL Server Database to reverse engineer it.

The Solution

SELECT t.[text], s.last_execution_time
FROM sys.dm_exec_cached_plans AS p
INNER JOIN sys.dm_exec_query_stats AS s
  ON p.plan_handle = s.plan_handle
  CROSS APPLY sys.dm_exec_sql_text(p.plan_handle) AS t
WHERE t.[text] LIKE N'%something unique about your query%'
ORDER BY s.last_execution_time DESC;

Read more