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
Step 5: Get Log Sequence Number of the LOP_BEGIN_XACT Log Record
Step 6: Recover Deleted Records in SQL Server
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
Post a Comment