I need to see which user has made what query with a lookback of ~90(?) days.
I have activated change tracking on the database with 90 days.
I have found several solutions:
SELECT
deqs.*,
dest.*,
deqs.last_execution_time AS [Time],
dest.TEXT AS [Query]
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
But this query doesn't give me any information about the user who made the query.
Then I got this query
SELECT
*
FROM
FN_TRACE_GETTABLE('E:\MSSQLServer\MSSQL12.DBNAME\MSSQL\Log\log_29.trc', default)
WHERE
TextData IS NOT NULL
ORDER BY
TransactionID DESC
This give me the username but no statements about DELETE/UPDATE etc.
I read about the SQL Profiler but I can't copy/read the _log.mdf-files because it is always in use.
I need a tip what s the right way to do this.. :/
Best Answer
Change tracking provides an efficient, light-weight data change tracking mechanism. In other words, this only tracks
UPDATE, INSERT, DELETE
statements; it does not trackSELECT
statements. It does not track who made the change.If you truly need to track all DML and DDL statements made against the server, you'll need to implement an Extended Events monitoring solution, but be aware for a server that is heavily used, you will likely affect performance in a negative way.
If you only are interested in DML changes, then change tracking will allow you to understand what changes were made to the data using the
CHANGETABLE
T-SQL statement. This sample code shows how that works. The code drops any database named ChangeTrackingTest first, then creates that same database with a single table that has change tracking enabled.The results:
Cleanup: