How to Repair a Corrupted SQL Server Database (MDF and NDF) Without Losing Data

Author : SysInfo Microsoft 365 Tenant to Tenant Migration | Published On : 29 Sep 2026

A corrupted SQL Server database can stop applications, halt reports, and put business-critical data at risk. Whether the cause is a sudden power cut, a failing disk, or a malware attack, the first question is always the same: can the data be recovered? In most cases it can. This guide covers the common causes of SQL database corruption, the errors you may see, the built-in fixes, and when a dedicated tool such as Aryson SQL Database Recovery makes sense.

What Causes SQL Database Corruption?

SQL Server stores data in primary (.mdf) and secondary (.ndf) files. When these files are damaged, the database may refuse to open or attach. Common causes include:

  • Hardware failure, especially bad disk sectors or a faulty storage controller
  • Unexpected shutdowns or power outages during a write operation
  • Insufficient disk space or memory
  • Malware or ransomware attacks
  • Damaged transaction log files
  • Bugs or failed upgrades in the SQL Server environment

Common Error Messages

If your database is corrupt, you will often see one of these:

  • Error 5171: the file is not a primary database file.
  • Error 5172: the file header is not a valid database header.
  • Error 5173: attach database failed.
  • Consistency errors: "DBCC CHECKDB reported database consistency errors."
  • I/O errors: incorrect checksum or torn page detected.
  • FCB error: could not open the File Control Bank for an invalid file ID.

Each of these points to physical or logical damage inside the database files, and each needs a different level of repair.

Built-In Methods and Their Limits

Fixing "Recovery Pending" state

Check for disk space and storage problems first. Then set the database to emergency mode with ALTER DATABASE and run DBCC CHECKDB to look for corruption. This often clears the state but cannot guarantee complete recovery.

Recovering from suspect mode

Damaged log files, corruption, or a full disk can push a database into suspect mode. Microsoft's manual steps can bring it back in simple cases, but they carry risk when the damage is severe.

DBCC CHECKDB with REPAIR_ALLOW_DATA_LOSS

This is the last-resort option built into SQL Server, and the name is a warning. It fixes structural problems by removing whatever it cannot repair, so you may lose rows or pages permanently. Always take a copy of the database files before running it.

Restoring from backup

If you have a recent, healthy backup, restoring is the safest route. Problems start when the backup is old, missing, or also corrupt.

When a Recovery Tool Makes Sense

If there is no usable backup, or the built-in commands fail or would cause data loss, a specialised tool is the practical next step. Aryson SQL Database Recovery is designed to repair damaged MDF and NDF files and rebuild the database objects inside them.

Key capabilities

  • Dual scan modes: Standard mode handles minor damage, while Advanced mode digs deeper for severe corruption.
  • Wide object recovery: it retrieves tables, views, stored procedures, triggers, functions, indexes, rules, and keys.
  • Deleted record recovery: removed table rows can be recovered from the damaged file.
  • Compressed data support: it handles ROW and PAGE compressed tables.
  • Preview before saving: you can inspect recovered objects and choose exactly what to save.
  • Flexible export: save into a new or existing SQL Server database, as SQL-compatible scripts with schema and data, or as CSV.
  • Authentication options: it works with both Windows and SQL Server authentication.
  • Non-destructive process: the original file stays untouched, and recovered data goes into a new database.
  • Version support: it covers a broad range of SQL Server releases, from legacy versions up to recent ones (check the vendor's page for the current supported list).

Note that Microsoft SQL Server must be installed, because the tool writes recovered data into a new database.

Step-by-Step: Recovering an MDF File

  1. Download, install, and launch the software (run it as Administrator on modern Windows).
  2. Click Browse and select the corrupted MDF file.
  3. Choose Standard or Advanced scan mode and click OK.
  4. Wait for the scan, then select the file to preview the recovered objects.
  5. Enter your SQL Server credentials and test the connection.
  6. Click OK and save the recovered data to your chosen destination.

Free Demo vs. Full Version

The demo lets you scan a corrupted database and preview the recovered objects, which is useful for confirming your data is recoverable before you buy. Saving the recovered data requires the licensed version.

Best Practices to Prevent Future Corruption

  • Schedule regular full, differential, and log backups, and test restores periodically.
  • Run DBCC CHECKDB on a schedule to catch problems early.
  • Use a UPS to protect servers from power loss.
  • Monitor disk health and free space.
  • Keep SQL Server patched and use reliable antivirus and firewall protection.
  • Store backups on a separate system from the live database.

Final Thoughts

Database corruption is stressful, but it is rarely the end of your data. Start with backups and Microsoft's built-in commands, and be careful with anything that permits data loss. When those options fail or no backup exists, a dedicated recovery tool can rebuild your tables, views, and other objects from the damaged MDF and NDF files. Whatever route you choose, work on a copy of the original files, and use the demo or preview to confirm recovery before committing.