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.
- USE master
- GO
- CREATE DATABASE TestDb
- GO
- USE TestDb
- GO
- CREATE TABLE TestTable (
- id INT identity(1, 1)
- ,NAME VARCHAR(50)
- ,Value INT
- ,ValueAt DATETIME DEFAULT(GetDate())
- )
- GO
Create a full backup of this database.
- BACKUP DATABASE TestDb TO DISK = 'D:\TestDb_Full.bak'
- WITH init
- ,format
- ,stats = 10
- GO
Follow the below DML on table dbo.TestTable.
- USE TestDb
- GO
- INSERT INTO TestTable ( NAME,Value)
- SELECT 'Vimal' ,50
- UNION
- SELECT 'Kamal' ,250
- UNION
- SELECT 'Raj' ,1200
- UNION
- SELECT 'Swastika',150
- UNION
- SELECT 'Sibin' ,125
- UNION
- SELECT 'Manas' ,560
- UNION
- SELECT 'Amit' ,3200
- GO
- SELECT * FROM TestTable
- GO
- DELETE FROM TestTable WHERE id > 5
- GO
- SELECT * FROM TestTable
- 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.
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.
- SELECT [Current LSN]
- ,[Transaction ID]
- ,[Operation]
- ,[Context]
- ,[AllocUnitName]
- FROM fn_dblog(NULL, NULL)
- WHERE [Operation] = 'LOP_DELETE_ROWS'
- AND [AllocUnitName] = 'dbo.TestTable'
- SELECT [Current LSN]
- ,[Operation]
- ,[Transaction ID]
- ,[Begin Time]
- ,[Transaction Name]
- ,[Transaction SID]
- ,[AllocUnitName]
- FROM fn_dblog(NULL, NULL)
- WHERE [Transaction ID] = '0000:00000343'--<<Paste the TransactionID
- AND [Operation] = 'LOP_BEGIN_XACT'





Krish gurujuPosted Jul 25, 2018, 2:32 PM
Nice, well done...