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.

 


Was this answer helpful?

« Back

chat