Today, I will tell you how to recover the data, in the case of accidental deletion of the data. This operation is tested on Enterprise Edition 2012.

Please follow the steps and execute the next step only when the previous step is finished.

Step 1

Create a new fresh database and a table inside this database.

  1. USE master
  2. GO
  3. CREATE DATABASE TestDb
  4. GO
  5. USE TestDb
  6. GO
  7. CREATE TABLE TestTable (
  8. id INT identity(1, 1)
  9. ,NAME VARCHAR(50)
  10. ,Value INT
  11. ,ValueAt DATETIME DEFAULT(GetDate())
  12. )
  13. GO
Step 2

Create a full backup of this database.
  1. BACKUP DATABASE TestDb TO DISK = 'D:\TestDb_Full.bak'
  2. WITH init
  3. ,format
  4. ,stats = 10
  5. GO
Step 3

Follow the below DML on table dbo.TestTable.
  1. USE TestDb
  2. GO
  3. INSERT INTO TestTable ( NAME,Value)
  4. SELECT 'Vimal' ,50
  5. UNION
  6. SELECT 'Kamal' ,250
  7. UNION
  8. SELECT 'Raj' ,1200
  9. UNION
  10. SELECT 'Swastika',150
  11. UNION
  12. SELECT 'Sibin' ,125
  13. UNION
  14. SELECT 'Manas' ,560
  15. UNION
  16. SELECT 'Amit' ,3200
  17. GO
  18. SELECT * FROM TestTable
  19. GO
  20. DELETE FROM TestTable WHERE id > 5
  21. GO
  22. SELECT * FROM TestTable
  23. GO
Step 4

As the data has been deleted, now it's time to recover that data.

Note -
Here, database is new, so is the table. It is easy to recover. If you know the time of the deleted data or approximate time, you can recover the data easily.
But, you will find it difficult if the time range is too long or you have forgotten the time.
  1. SELECT [Current LSN]
  2. ,[Transaction ID]
  3. ,[Operation]
  4. ,[Context]
  5. ,[AllocUnitName]
  6. FROM fn_dblog(NULL, NULL)
  7. WHERE [Operation] = 'LOP_DELETE_ROWS'
  8. AND [AllocUnitName] = 'dbo.TestTable'
  9. SELECT [Current LSN]
  10. ,[Operation]
  11. ,[Transaction ID]
  12. ,[Begin Time]
  13. ,[Transaction Name]
  14. ,[Transaction SID]
  15. ,[AllocUnitName]
  16. FROM fn_dblog(NULL, NULL)
  17. WHERE [Transaction ID] = '0000:00000343'--<<Paste the TransactionID
  18. AND [Operation] = 'LOP_BEGIN_XACT'
  1. --Copy CurrentLSN 00000021:000000b0:0001
  2. SELECT CONVERT(INT, CONVERT(VARBINARY, '0x00000021', 1)) --Same Value
  3. SELECT CONVERT(INT, CONVERT(VARBINARY, '0x000000b0', 1)) --Total 10 digit preceeded by 0
  4. SELECT CONVERT(INT, CONVERT(VARBINARY, '0x0001', 1)) --Total 5 digit preceeded by 0
  5. -->33000000017600001
  6. SELECT Cast(CONVERT(INT, CONVERT(VARBINARY, '0x00000021', 1)) AS VARCHAR(5)) + RIGHT('0000000000' + CAST(CONVERT(INT, CONVERT(VARBINARY, '0x000000b0', 1)) AS NVARCHAR), 10) + RIGHT('00000' + CAST(CONVERT(INT, CONVERT(VARBINARY, '0x0001', 1)) AS NVARCHAR), 5) AS 'MarkPoint'
  7. GO

Step 5

Take a log backup now. For this, the recovery model of database should be Full or Bulk-logged (Right click on Database>>Properties>>Options>>Recovery Model).
  1. BACKUP log TestDb TO DISK = 'D:\TestDb_log.trn'
  2. GO
Step 6

Restore Full backup of database.
  1. -- Starting first with restoring the FULL BACKUP with NORECOVERY
  2. RESTORE filelistonly
  3. FROM DISK = 'D:\TestDb_Full.bak';
  4. RESTORE DATABASE [TestDb_New]
  5. FROM DISK = 'D:\TestDb_Full.bak'
  6. WITH MOVE 'TestDb' TO 'C:\TestDb.mdf'
  7. ,MOVE 'TestDb_log' TO 'C:\TestDb_log.ldf'
  8. ,REPLACE
  9. ,NORECOVERY;
  10. GO
Step 7

Restore log backup of database, like below.
  1. RESTORE LOG TestDb_New
  2. FROM DISK = 'D:\TestDb_log.trn'
  3. WITH STOPBEFOREMARK = 'lsn:33000000017600001'
  4. GO
Step 8

Check the table data.
  1. USE TestDb_New
  2. GO
  3. SELECT * FROM dbo.TestTable
  4. --Export the deleted rows from this table to original table


Step 9

Drop tables.
  1. USE master
  2. GO
  3. DROP DATABASE TestDb
  4. DROP DATABASE TestDb_New
  5. -->The End<--
Step for time range.
  1. SELECT [Current LSN]
  2. ,[Operation]
  3. ,[Transaction ID]
  4. ,[Begin Time]
  5. ,[Transaction Name]
  6. ,[Transaction SID]
  7. ,[AllocUnitName]
  8. FROM fn_dblog(NULL, NULL)
  9. WHERE --[Operation] = 'LOP_BEGIN_XACT'
  10. --and
  11. [Begin Time] BETWEEN '2015/07/17 15:30:00:000'
  12. AND '2015/07/17 16:00:00:000'