File Repair
Database Recovery with Piecemeal Restore in Microsoft SQL Server
July 28, 2026| 7 min read
Summary: Corruption in the primary database in an Availability Group can extend to the secondary replicas, leading to errors during failover and causing significant downtime. In this blog, we will see how to repair a corrupted SQL database in Always-On Availability Groups. To repair the corrupted database file without data loss, you can use the SQL repair tool mentioned in this article.
Free Download 100% SecureMS SQL Server offers different features, like database mirroring, failover clusters, etc. to restore database in case of server failure or disaster. However, database mirroring does not support automatic failover and disaster recovery solutions. On the other hand, failover clusters require shared storage resource.
To overcome the limitations of database mirroring and failover clusters, you can use the Always on Availability groups feature in SQL Server. This feature is a combination of data mirroring and failover clustering. It provides both high availability and disaster recovery solutions on an enterprise-level. An Always on availability group supports a failover environment. It contains a set of databases known as availability groups that can failover together and replicate. In this, a discrete set of user databases is deployed in different standalone SQL server instances. It uses a local disk instead of the shared disk as a resource.
In availability group, changes made to the primary database are replicated to the secondary databases (replicas) using the log shipping feature. If any database in the group is corrupted or damaged, the corrupted data is replicated to the secondary databases. In such a case, you can face failover issues or failed synchronization, and may impact availability of the entire group.
In this article, we will discuss how to repair corrupt SQL databases in Always on Availability Groups.
You can't restore or repair the database, which is an active part of an availability group. To repair the damaged database, you are required to first remove that database from the group, repair it, and then restore it on primary and secondary databases. After this, add the repaired database to the Availability Group. Here's how to do so:
To remove database from the Availability Group, follow the below steps:
Alternatively, you can use the ALTER AVAILABILITY Group statement to remove the database (see the below example).
ALTER AVAILABILITY GROUP availabilitygroupname REMOVE DATABASE Databasename;
After removing the corrupted database from the Availability Group, you can either restore the backup copy (if available) or repair the database.
1. Restore the Database from Backup
Here are the steps to restore the database from backup using the SSMS:
Alternatively, you can also use T-SQL queries to restore the database from the .bak file. For this,
USE [master]
RESTORE DATABASE [bank121] FROM DISK = N'D:\Internal\BackupfileRD.BAK'
WITH FILE = 1, MOVE N'bank121' TO N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\bank121.mdf',
MOVE N'bank121_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\bank121_log.ldf',
NOUNLOAD, STATS = 5
GO
Now, restore the database on secondary replicas. Before restoring the backup make sure your backup is readable by using the RESTORE VERIFYONLY statement as given below.
RESTORE VERIFYONLY FROM DISK = C:\YourDatabase.BAK GO
The above command will help you verify that your backup file is not corrupted and can be restored without any problem.
After that, manually prepare your secondary database for an availability group.
2. Repair the Corrupt Database
If the backup is not readable or not available, then you can try to repair the corrupt database by using the DBCC CHECKDB command. You can use the REPAIR_ALLOW_DATA_LOSS option with the command. Here are the steps:
Note: Before running the command, make sure you have all the permissions and privileges on the database.
Step 1: Set the Database to SINGLE_USER Mode
First, you need to set the database to SINGLE_USER mode. This will prevent other users from changing the data during the repair process. Here’s the command:
Step 2: Run DBCC CHECKDB Command
After setting the database to SINGLE_USER mode, run the DBCC CHECKDB command with the REPAIR_ALLOW_DATA_LOSS option as given below:
DBCC CHECKDB (N ’Dbtesting11’, REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS, NO_INFOMSGS;
GO
Note: The REPAIR_ALLOW_DATA_LOSS option can lead to data loss as it may deallocate data, such as rows, pages, or series of pages, while resolving the issues
Step 3: Set the Database to MULTI-USER Mode
When the database is repaired, set the database again to MULTI-USER mode. Here’s how:
The DBCC CHECKDB command can fix database consistency issues, but it may not guarantee complete data recovery and can leave your database in a logically inconsistent state. To prevent such downsides, you can use the Stellar Repair for MS SQL. This tool can repair corrupt MS SQL database quickly and recover all the objects with complete integrity and precision. The tool can also recover deleted records from the corrupted/damaged SQL database file
Features of Stellar Repair for MS SQL
After restoring or repairing the database, you can now add it to Always on Availability Group. Here are the steps:
Next, use the Availability Group Properties window and configure the corresponding secondary database on each server instance. For detailed information, read Start Data Movement on an Always On Secondary Database (SQL Server)
If any of the databases in Availability Group gets corrupted or damaged, you need to remove the database from the group and repair it. To quickly and easily repairing the database, you can use Stellar Repair for MS SQL. It can repair the database and restore all the data from corrupt SQL database files with complete integrity and precision. It allows you to save the repaired data to a new database file, which you can easily add to the Availability Group.