HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to find DELETE and UPDATE statements that have been executed on a database?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
updatehowdeletestatementsbeendatabaseexecutedthatfindand

Problem

I am trying to find out what delete and update statements have been executed on a certain database in a certain period of time.

I came across this following query, but it generates an error message:

SELECT 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
where dest.TEXT like '%Delete%from%'
ORDER BY deqs.last_execution_time DESC


The error message is:


Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '.'.

If I try:

SELECT 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(sql_handle) AS dest
where dest.TEXT like '%Delete%from%'
ORDER BY deqs.last_execution_time DESC


I get another error:

Msg 321, Level 15, State 1, Line 3
"sql_handle" is not a recognized table hints option. If it is intended as a parameter to a table-valued function or to the CHANGETABLE function, ensure that your database compatibility mode is set to 90.


As far as I understand in my research of the topic it is not an easy task to achieve.

Is this query even the right approach?

SQL VERSION in question:

Microsoft SQL Server Standard Edition (64-bit)
Microsoft SQL Server Management Studio                      10.0.2531.0
Microsoft Analysis Services Client Tools                        10.0.1600.22
Microsoft Data Access Components (MDAC)                     6.1.7600.16385
Microsoft MSXML                     3.0 6.0 
Microsoft Internet Explorer                     8.0.7600.16385
Microsoft .NET Framework                        2.0.50727.4984
Operating System                        6.1.7600

Solution

Run this in the context of the master database as you must be running it under the context of a user database set to SQL Server 2000 compatibility level.

The following should work fine

USE master;

SELECT 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
WHERE  dest.text LIKE '%Delete%from%'
ORDER  BY deqs.last_execution_time DESC

Code Snippets

USE master;

SELECT 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
WHERE  dest.text LIKE '%Delete%from%'
ORDER  BY deqs.last_execution_time DESC

Context

StackExchange Database Administrators Q#36149, answer score: 8

Revisions (0)

No revisions yet.