I need to write a query to get the statements executed by the user in a particular database since the last restart.
First of all, I extract all the transactions from ::fn_dblog(NULL,NULL), which returns the transaction log. After this, I tried to get the cached queries using this two queries:
SELECT *
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;
-- OR
select * from master.dbo.syscacheobjects where dbid = db_id() and objtype = 'Adhoc';
My idea is to join the statements that can produce a transaction with its transaction, and show the others too. The problem is that the first query that tries to get the information of the queries doesn't provide any information to identify the database, and the other query doesn't provide any information that I can use to join it to the transaction log.
I'm trying to construct this query without previous knowledge of this particular DBMS, so please forgive my conceptual errors. Thank you in advance.