How to Repair MDF Files in SQL Server Database?

Important Note :  Before proceed to following steps, we strongly advise taking your database backup without fail. You need to proceed it at your own risk.

We will use the DBCC (database console commands) commands in the SQL server management studio to repair the corrupt MDF files of the SQL database.

There are many DBCC commands to check and repair the SQL database like DBCC CHECKDB, DBCC DBREPAIR.

To fix the corrupt MDF files in the database follow the below steps.

  1. Open the SQL server management studio.

  2. Now click on New query.

  3. Write the below script in query and execute it.

    EXEC sp_resetstatus [YourDatabase];
    ALTER DATABASE [YourDatabase] SET EMERGENCY
    DBCC CHECKDB ([YourDatabase], REPAIR_ALLOW_DATA_LOSS)
    ALTER DATABASE [YourDatabase] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    DBCC DATABASE ([YourDatabase], REPAIR_ALLOW_DATA_LOSS)
    ALTER DATABASE [YourDatabase] SET MULTI_USER
    

Replace the YourDatabase with your actual database name. Once you execute it there should be no more corrupt MDF files in your database.

 

  • 0 Users Found This Useful

Was this answer helpful?

Related Articles

How to Fix Error Forcing close of thread 310 user: 'root' in MySQL?

Problem Statement MySQL server keeps on shutting down. Error Message C:\Program...

MySQL Connection Errors and Troubleshooting

This article will assist you to fix the common causes of MySQL connection errors along with steps...

How to check MYSQL Database size in phpMyAdmin and via command?

Sometimes it is required to check the MySQL Database size. You can check it from the phpMyAdmin...

How to rename MySQL Database table from phpMyAdmin?

It’s very easy to rename a table in phpMyAdmin. To do so, simply follow these steps: MySQL...

How to convert time zone in MySQL?

You can change the timezone in MySQL using php function CONVERT_TZ. Following is the SQL command...