Published on

May 14, 2021

How to Repair a SQL Database in SQL Server Management Studio

As a SQL database administrator, it is crucial to maintain and secure databases. However, there may be instances where you encounter database corruption. If not addressed promptly and correctly, you risk losing valuable data. In this article, we will discuss how to repair a database in SQL Server Management Studio (SSMS) when there is no backup. Additionally, we will explore an alternative solution to repair a SQL database without the risk of data loss.

Possible Reasons for SQL Database Corruption

There are several factors that can lead to database corruption:

  • Server crashes or abrupt system shutdowns during data processing
  • I/O subsystem failures
  • Bugs in the SQL software

If you encounter hardware-related issues, it is advisable to seek support from your hardware vendor. For SQL software bugs, check for updates released by Microsoft to address these issues. However, in the event of a system crash or sudden shutdown, you will need to repair the corrupted database to recover its data.

Before We Proceed

Before we begin the repair process, ensure that your system meets the following prerequisites:

  • SQL Server Management Studio (SSMS) must be installed on your machine. If it is not installed, you can download and install it from the official Microsoft website.
  • SQL Server must be installed on your PC.

Steps to Repair a Database in SQL Server Management Studio

  1. Launch SQL Server Management Studio and connect to a server instance.
  2. In the Object Explorer window, click the ‘+’ sign to expand the databases.
  3. Right-click on the database you want to repair and select Properties.
  4. In the ‘Database Properties’ dialog box, click the Options tab from the left pane.
  5. Scroll down to the bottom of the Options screen.
  6. To repair the SQL database, set it to single-user mode by selecting ‘Restrict Access’ to SINGLE_USER from MULTI_USER. Click OK.
  7. Alternatively, you can set the database to single-user mode by executing the following query:

    ALTER DATABASE [DatabaseName] SET SINGLE_USER;

    Replace [DatabaseName] with the name of your corrupted database.

  8. To repair the SQL database, run the DBCC CHECKDB command with one of the following repair options: REPAIR_FAST, REPAIR_REBUILD, or REPAIR_ALLOW_DATA_LOSS.
  9. Here is an example of using the DBCC CHECKDB command with the ‘REPAIR_ALLOW_DATA_LOSS’ option:

    DBCC CHECKDB ([DatabaseName], REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS, NO_INFOMSGS;

    Replace [DatabaseName] with the name of your corrupted database.

  10. Click the Execute button to run the query and repair the database.
  11. Change the database mode from ‘SINGLE_USER’ to ‘MULTI_USER’ by following steps 4-6 above.
  12. Close and relaunch SQL Server Management Studio for the changes to take effect.

How to Recover Missing Data after Running DBCC CHECKDB

If you discover missing data in the repaired database after running the DBCC CHECKDB command with the REPAIR_ALLOW_DATA_LOSS option, consider using a SQL repair tool. Stellar Repair for MS SQL is a recommended software solution used by database administrators (DBAs) and Microsoft MVPs worldwide. It can handle corruption errors in a SQL database and recover severely corrupted MDF/NDF files on both Windows and Linux systems. The software ensures the recovery of all database objects, such as tables, views, keys, stored procedures, etc., without altering the original database structure. It also facilitates the recovery of deleted records.

Conclusion

This article has outlined the common reasons for SQL database corruption. While restoring from a backup is the recommended approach to address database corruption, there are instances where the backup may not be up to date or has become corrupt. In such cases, it is necessary to attempt repairing the database. The step-by-step instructions provided in this article guide you through the process of repairing a database in SQL Server Management Studio. Additionally, we have suggested using a SQL recovery tool as an alternative to the DBCC CHECKDB command with the REPAIR_ALLOW_DATA_LOSS option, which carries the risk of data loss.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.