How to Recover Deleted Table Records in SQL Server

 A log sequence number (LSN) is a unique identifier for each record in a SQL Server transaction log. The LSN of a log record at which an important event happened can be used to build restore sequences.

To further understand how to restore deleted table entries using the log sequence number method, let's look at an example. In this example, a database and table will be created, and rows will be added, deleted, and information about the destroyed data will be obtained. Finally, the data will be recovered.

How to Use LSN to Retrieve Deleted Records from a SQL Table

We will construct a test database and a table in this part, and then we will perform a DELETE operation on it. Next, we will use LSN to locate deleted data and retrieve it.

Step 1: Create a Database

Execute the following query to create a database named ‘RecoverDeletedRecords’ and a table named ‘Employee’:

USE [master];   GO    CREATE DATABASE RecoverDeletedRecords; GO USE RecoverDeletedRecords;    GO CREATE TABLE [Employee] ( [Sr.No] INT IDENTITY, [Date] DATETIME DEFAULT GETDATE (), [City] CHAR (25) DEFAULT 'City1');

Step 2: Insert Data into Table

We have made two tables: one with three columns called "Employee" and the other with the name "RecoverDeletedRecords." We will now execute the following query to add rows to the table:

USE RecoverDeletedRecords; GO INSERT INTO Employee DEFAULT VALUES; GO 25

Step 3: Delete Rows from Table

USE RecoverDeletedRecords Go DELETE Employee WHERE [Sr.No] < 10 GO Select * from Employee

When you are done with deleting the records, then a log is made with your transaction type, an action which you performed along with action Id.

Next we will do to get the information about the deleted rows:
USE RecoverDeletedRecords GO SELECT  [Current LSN],     [Transaction ID],      Operation,      Context,      AllocUnitName FROM     fn_dblog(NULL, NULL) WHERE     Operation = 'LOP_DELETE_ROWS'

Here after executing this command you will get the transaction id and the transaction time when you deleted the records. With the help of transaction time we can get the exact records which were deleted:

Step 5: Get Log Sequence Number of the LOP_BEGIN_XACT Log Record

USE RecoverDeletedRecords GO SELECT  [Current LSN],     Operation,      [Transaction ID],      [Begin Time],      [Transaction Name],      [Transaction SID] FROM     fn_dblog(NULL, NULL) WHERE     [Transaction ID] = '0000:0000020e' AND     [Operation] = 'LOP_BEGIN_XACT'

After executing this command you will get Transaction Time and Transaction Name. Now let's follow the below step to recover the data:

Step 6: Recover Deleted Records in SQL Server

The LSN values must be converted from hexadecimal to decimal form in order to retrieve the deleted SQL database records.  To accomplish this, put "0x" before the log sequence number, as the following code illustrates:

--Restoring Full backup with norecovery. RESTORE DATABASE RecoverDeletedRecords_COPY     FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50\MSSQL\Backup\RecoverDeletedRecords.bak' WITH     MOVE 'RecoverDeletedRecords' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50\MSSQL\Backup\RecoverDeletedRecords.mdf',     MOVE 'RecoverDeletedRecords_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50\MSSQL\Backup\RecoverDeletedRecords.ldf',     REPLACE, NORECOVERY;     GO --Restore Log backup with STOPBEFOREMARK option to recover exact LSN.    RESTORE LOG RecoverDeletedRecords_COPY FROM     DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.STELLAR\MSSQL\Backup\RecoverDeletedRecords_tlogbackup.trn' WITH     STOPBEFOREMARK = 'lsn:0x00000014:0000001a:0001'

Step 6: Now run the command to check

USE RecoverDeletedRecords_COPY GO SELECT * from Employee

Note: In the code above, replace the path ‘C:\Program Files\Microsoft SQL Server\MSSQL10_50\MSSQL\Backup\RecoverDeletedRecords.bak’ with the path where your backup file is located. Also, replace the path of .mdf and .ldf files with the path where you have saved the database files.

Comments

Popular posts from this blog

Ag Grid Angular

.NET Watcher in a .NET Core API

Power Shell UI Forms