12

Is it possible to view delete statements that have recently occurred in the Transaction Log?

11

you wont find the exact scripts that were executed on sql.(in the transaction log)

A transaction log is a file that contains information regarding every change that has been made to the database. This includes data modifications (transactions), database modifications, and backup/restore events.

The primary purpose the transaction log is to provide a method to be able to restore a database to a point-in-time when necessary. This can include rolling back transactions to a certain time, or to roll forward transactions from a full backup restoration.

to know more about Transaction log http://www.sqlservercentral.com/articles/Design+and+Theory/63350/

here is a script to show you recent ran delete queries

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
  • FYI: FROM is an optional keyword in a DELETE statement, so I wouldn't include it in the matching predicate here. – Jon Seigel May 29 '12 at 2:12
  • True the From in delete statement is optional, i kept it just to limit the results of my query. but yeah you are right – AmmarR May 29 '12 at 5:10
  • Note, for case-sensitive SQL Server instances, the code above will miss delete and DELETE, etc. You may want to make the where clause case-insensitive, for instance: WHERE dest.text LIKE '%DELETE %' COLLATE Latin1_General_CI_AS – Max Vernon Aug 18 '16 at 15:30
11

You can view the individual rows that were deleted by looking for LOP_DELETE_ROWS operations still in the log:

select * from fn_dblog(NULL, NULL) 
where Operation = 'LOP_DELETE_ROWS'

If the log was recycled (in simple recovery model) or truncated by backup (in full or bulk recovery model) then you will only be able to see the log operations still available in the log.

Understanding the log operations is quite complex, you need to be aware of things like undo or compensating operations to make sense of some log patterns you can encounter, but straight forward committed DELETEs are fairly easy to comprehend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.