Home » Blog » SQL Server » Repair a Corrupt SQL MDF File in 5 Ways

Repair a Corrupt SQL MDF File in 5 Ways

Neeraj Kumar
Published By Neeraj Kumar
Debasish Pramanik
Approved By Debasish Pramanik
Published On
Last Updated

SSMS has no button to repair corrupt SQL MDF file damage and DBCC CHECKDB is the only native repair. Five methods include a backup restore, REPAIR_REBUILD, an accepted data loss repair, an emergency mode copy and recovery software. Free repair options delete rows when they must and some error types have no repair option at all. Recovery software extracts the objects into a new database so preview the tables and test one export first.

Corrupt MDF files stop your database from opening and queries fail with checksum or consistency errors. Primary data files store startup information for the database and point to every other file.

This guide explains how to repair a corrupt SQL MDF file and the tests that show whether the MDF file or the log file needs the fix.

What Does an MDF File Store in SQL Server?

MDF is the extension of the primary data file of a SQL Server database. Microsoft states that this file contains the startup information for the database and points to the other files.

Tables and other objects live in the MDF file or in secondary NDF files. LDF files store the transaction log.

What Causes MDF File Corruption?

SQL Server finds damaged pages through page verification. Microsoft states that disk I/O path errors come mostly from power failures or disk hardware failures at the moment SQL Server saves a page.

  • Power Failures. A power cut during a page save produces a half finished page.
  • Disk Hardware Failures. Failing disks and controllers return bad data to SQL Server.
  • I/O Path Problems. Error 824 reports a checksum failure and Microsoft says it indicates an I/O path problem.
  • Disabled Page Verification. PAGE_VERIFY set to NONE hides damage until a query touches the page.
  • Interrupted File Copies. A copy of the MDF file that stops midway produces a damaged file.

How to Confirm MDF File Corruption?

Start with a DBCC CHECKDB check that has no repair option. Microsoft recommends this order because the output names the repair level that the errors need.

DBCC CHECKDB (N'PayrollDB');

Every finished check ends with allocation and consistency error counts. Zero errors mean the MDF file is healthy.

Look at the page verification setting next.

SELECT name, page_verify_option_desc AS verify_mode
FROM sys.databases
WHERE name = N'PayrollDB';

Error 824 in the SQL Server error log is a checksum failure. Microsoft says SQL Server retries a failed page four times before it raises the error.

What to Check Before You Repair a Corrupt SQL MDF File?

Protect the file before the first command.

  • Duplicates. Stop the SQL Server service and place duplicates of the primary, secondary and log files on a separate disk.
  • Backup. Find your newest full backup and test it with RESTORE VERIFYONLY.
  • Log File. Confirm that the LDF file exists because some repairs need it.
  • Access. Sign in as a member of sysadmin or db_owner.

RESTORE VERIFYONLY checks that the backup set has all its volumes and valid page headers. Microsoft states that it does not verify the structure of the data in the backup, so a test restore gives stronger proof.

Which Free Methods Repair Corrupt MDF Files?

Four free methods repair corrupt SQL MDF file damage or rescue the data. Methods are ordered by risk.

Restore a Tested Backup (Free, Safest)

Restores swap the damaged file for a healthy one. You lose edits made after the backup unless log backups exist.

RESTORE VERIFYONLY
FROM DISK = N'D:\SQLBackups\PayrollDB_full.bak'
WITH CHECKSUM;

RESTORE DATABASE [PayrollDB]
FROM DISK = N'D:\SQLBackups\PayrollDB_full.bak'
WITH REPLACE, RECOVERY;

Follow the BAK Restore Steps guide when you prefer the SSMS wizard.

Rebuild Indexes With REPAIR_REBUILD (Free)

Microsoft designs REPAIR_REBUILD for repairs without any chance of data loss, from quick fixes like missing rows in nonclustered indexes to slower fixes like index rebuilds.

ALTER DATABASE [PayrollDB] SET SINGLE_USER;
DBCC CHECKDB (N'PayrollDB', REPAIR_REBUILD);
ALTER DATABASE [PayrollDB] SET MULTI_USER;

Check again without a repair option and expect zero errors.

Accept Data Loss to Finish the Repair (Free, Last Resort)

Choose this method when REPAIR_REBUILD reports remaining errors and no backup exists. Microsoft warns that the data loss option removes unrepairable pages and rows.

Save duplicates of the files first. Then wrap the repair in a transaction so you can reject the result.

ALTER DATABASE [PayrollDB] SET SINGLE_USER;
BEGIN TRANSACTION;
DBCC CHECKDB (N'PayrollDB', REPAIR_ALLOW_DATA_LOSS);
COMMIT TRANSACTION;

Afterward check referential integrity with DBCC CHECKCONSTRAINTS and take a full backup. Our DBCC Repair Troubleshooting Guide explains why this repair fails.

Rescue Tables in Emergency Mode (Free)

Emergency mode opens a damaged database for sysadmin members only and marks it READ_ONLY. Copy every reachable table into a new database.

CREATE DATABASE [PayrollDB_Rescue];
ALTER DATABASE [PayrollDB] SET EMERGENCY;
SELECT * INTO [PayrollDB_Rescue].dbo.Employees FROM [PayrollDB].dbo.Employees;

Indexes and constraints need to be created again afterward. Emergency steps in full are in the Suspect Database Fix guide.

How to Repair a Corrupt SQL MDF File With EmailDoctor Software?

Open EmailDoctor SQL Database Recovery if DBCC fails and no backup exists. Software does not patch the damaged file. Scanning extracts stored procedures, tables, rules, views, triggers and functions into a new database.

Demo versions preview every object. Demo mode exports a 25 record sample of every table with 5 further objects.

Scanning needs 100 MB of disk space and .NET Framework 4.5 or newer on Windows 7 through 11.

Scan a duplicate of the MDF file so the original remains untouched.

Scan the Corrupt MDF File

  1. Double click the software icon to launch it. Recovery software home screen
  2. Click Open and find the duplicate of your MDF file. Open button Select MDF file window
  3. Set the scan mode to Quick Scan and select Auto detect for version detection. Specify the SQL Server version yourself if detection fails. Quick Scan option SQL Server version list
  4. Add NDF files on the NDF Options tab with auto detect or add them by hand using Add Files or Add Folder. NDF Options tab Add NDF files option
  5. Let the scan finish and confirm the Quick Scan note. Scan report Quick Scan note

Preview and Export to a New Database

  1. Select any table in the tree to display its rows. Recovered objects preview
  2. Press Export and select SQL Server Database. Export options
  3. Enter the server name manually or fetch it from the dropdown. Authenticate with Windows Authentication or a SQL Server login. SQL Server name list Windows Authentication option SQL Server Authentication option
  4. Select Create New Database and type a name that SQL Server does not use yet. Create New Database option Database name box
  5. Review the default collation and choose another one from the list when your source database used it. Default collation Collation list
  6. Tick the tables and objects you want in the new database. Database components list
  7. Set With Schema & Data and press Export. Schema and data options
  8. Close the Export Completed window with OK and dismiss the status window. Export Completed window Export status window
  9. Expand Databases in SSMS to check the repaired objects. Recovered database in SQL Server

    Switch to Advanced Scan when Quick Scan finds too few objects. Use SQL Database Viewer when you only need to look at the data because the viewer opens healthy and corrupt MDF files without repairing them.

Which Method Fits Your Damage?

Match your situation to the method.

DamageFixLossCost
Good backup existsRestoreEdits after the backupFree
Index or minor errorsREPAIR_REBUILDNoneFree
Severe errors without backupData loss repairRows and pagesFree
Database will not openEmergency copyIndexes and constraintsFree
Repair fails or no backupSoftware scanDemo sample onlyFree demo then paid

How to Prevent MDF File Corruption?

Four habits lower the risk of damaged files.

  • Page Verify. Microsoft makes CHECKSUM the default and recommends it for databases upgraded from older versions.
  • Regular Checks. Schedule DBCC CHECKDB so damage shows up before users notice.
  • Tested Backups. Take backups WITH CHECKSUM and restore them on a test server.
  • Stable Power. Connect database servers to a UPS because power failures damage pages during saves.

Set the recommended option with the command below.

ALTER DATABASE [PayrollDB] SET PAGE_VERIFY CHECKSUM;

Which Snags Slow Down an MDF Repair?

Use this list when a method fails.

  • Repair Option Refused. Switch the database to SINGLE_USER mode first.
  • Errors Remain After Repair. Restore a backup or use recovery software because the damage needs more than a repair option.
  • Backup Fails to Restore. RESTORE VERIFYONLY does not check data structure so test another backup.
  • Emergency Command Fails. Sign in as a sysadmin member because only that role uses emergency mode.
  • Scan Shows Few Tables. Switch to Advanced Scan and add the NDF files.

Frequently Asked Questions

Does SSMS Have an MDF Repair Button?

No. DBCC CHECKDB with a repair option is the native repair.

Can I Repair a Corrupt SQL MDF File Without a Backup?

Yes. Repair options, an emergency mode copy and recovery software all work without a backup. Backups remain the first choice when you have a good one.

Does Error 824 Mean MDF File Corruption?

Yes. Error 824 reports a checksum failure and Microsoft says it indicates an I/O path problem.

Does REPAIR_REBUILD Delete Data?

No. Its repairs are built to avoid data loss according to Microsoft.

Is PAGE_VERIFY CHECKSUM the Default?

Yes. Databases upgraded from older versions retain their old value so set it yourself.

Does RESTORE VERIFYONLY Prove Backup Quality?

No. Microsoft states that it does not verify the structure of the data in the backup.

Can I Rebuild the Log After LDF File Damage?

Yes when the database shut down cleanly. CREATE DATABASE with FOR ATTACH_REBUILD_LOG creates a new log and ends the old log backup chain.

Can I Open a Corrupt MDF File Without SQL Server?

Yes. SQL Database Viewer opens healthy and corrupt MDF files without a SQL Server installation.

Can Recovery Software Handle NDF Files Too?

Yes. Add the NDF files on the NDF Options tab before the scan.

Does the Demo Export Everything?

No. Demo mode limits tables to 25 records and other objects to 5.

Final Words

Corrupt MDF files start with a storage or power fault and show up as checksum errors. Confirm the damage with DBCC CHECKDB before you repair anything.

Restore a tested backup first and treat the data loss repair as the last free step. Enable CHECKSUM page verification so the next fault shows up early.

EmailDoctor software saves the objects of damaged MDF files in a fresh database.

  • You repair a corrupt SQL MDF file only after a diagnosis.
  • Backups and REPAIR_REBUILD lose no extra data and the data loss repair deletes rows.
  • CHECKSUM page verification finds damage early.
  • Recovery software saves the objects of a damaged file in a new database.
  Neeraj Kumar

By Neeraj Kumar

Neeraj Kumar is a Principal Technical Architect at EmailDoctor with 19+ years of experience in email migration, cloud migration, data recovery and digital forensics. He guides enterprise teams through Exchange, Office 365 and Google Workspace projects and shares what works as an EmailDoctor writer. Every method in his posts is tested on Windows and Mac before it goes live.