How to Repair SQL Database using DBCC CHECKDB Command?

Summary: You can use the DBCC CHECKDB command in SQL Server to check and repair corrupt SQL database. This posts explains how to use the DBCC CHECKDB command to repair the SQL database. It also mentions a specialized MS SQL repair software that can repair corrupt SQL database in just a few simple steps and without any data loss.

Database Console Command (DBCC) commands are used for database management and administration in SQL Server. The DBCC CHECKDB command is used to check the logical and physical integrity of SQL database. It helps identify and resolve corruption-related and structural issues in the SQL database.

How the DBCC CHECKDB Command Works?

The command thoroughly scans the database and performs integrity checks and other structural checks. If any of these checks fail, it displays consistency errors indicating issues in the database and also recommend appropriate repair option. Let’s take a look at the syntax of DBCC CHECKDB command.

DBCC CHECKDB    
    [ ( db_name | db_id | 0   
        [ , NOINDEX    
        | , { REPAIR_ALLOW_DATA_LOSS | REPAIR_FAST | REPAIR_REBUILD } ]   
    ) ]   
    [ WITH    
        {   
            [ ALL_ERRORMSGS ]   
            [ , EXTENDED_LOGICAL_CHECKS ]    
            [ , NO_INFOMSGS ]   
            [ , TABLOCK ]   
            [ , ESTIMATEONLY ]   
            [ , { PHYSICAL_ONLY | DATA_PURITY } ]   
            [ , MAXDOP  = number_of_processors ]   
        }   
    ]   
]

Now, let’s understand the various options used in the above DBCC CHECKDB command.

Steps to Repair SQL Database using the DBCC CHECKDB Command

To run the DBCC CHECKDB command, make sure you have the administrative privileges. Then, open the SQL Server Management Studio (SSMS) and follow these steps:

Note: We will be using database_name as Dbtesting. Make sure to replace ‘Dbtesting’ with the name of your database.

Step 1: Set Database to Emergency Mode

If the database is inaccessible, first change the database status to EMERGENCY mode. This will provide read-only access to the administrator. To put the database in EMERGENCY mode, run the following query in SSMS:

ALTER DATABASE [Dbtesting] SET EMERGENCY

Step 2: Check Database for Corruption Errors

After setting the database to EMERGENCY mode, execute the following command to check the database for corruption errors:

DBCC CHECKDB (Dbtesting) 

If the DBCC CHECKDB command detects any errors or corruption in the database, it will recommend an appropriate repair option.

Step 3: Set Database to SINGLE_USER Mode

Before using the repair option, you need to put the database in SINGLE_USER mode to prevent other users from modifying the data during the repair process. To set the database to SINGLE_USER mode, run the following T-SQL query in SSMS:

ALTER DATABASE Dbtesting SET SINGLE_USER 

Step 4: Repair the Database

After setting the database to SINGLE_USER mode, run the command with the REPAIR option recommended by DBCC CHECKDB command. If it shows an error message recommending to run the REPAIR_REBUILD as the minimum repair level, then run the DBCC CHECKDB command with REPAIR_REBUILD option as given below:

DBCC CHECKDB (' Dbtesting ', REPAIR_REBUILD)
GO

This command will rebuild the database without any data loss. It can repair missing rows in non-clustered indexes and help in fixing minor corruption errors. However, it is a time-consuming option.

Alternatively, you can use the REPAIR_FAST with the command as given below:

DBCC CHECKDB (' Dbtesting ', REPAIR_FAST)
GO

This repair option only maintains backward compatibility syntax and does not perform any repair actions.

If the above repair options fail or the DBCC CHECKDB command recommended using the REPAIR_ALLOW_DATA_LOSS repair option, then run the DBCC CHECKDB command as given below:

DBCC CHECKDB (N ’Dbtesting’, REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS, NO_INFOMSGS; 
GO

The REPAIR_ALLOW_DATA_LOSS repair option helps in repairing all reported errors in the SQL server database but it causes data loss. In fact, Microsoft recommends using the REPAIR_ALLOW_DATA_LOSS option as a last resort.

Step 5: Set Database to MULTI_USER Mode

After successfully repairing the database, set the database to MULTI_USER mode by executing the following command:

ALTER DATABASE Dbtesting SET MULTI_USER

Downsides of DBCC CHECKDB Command

Though the DBCC CHECKDB command can fix database consistency issues, you have to consider the following downsides when using the command with the REPAIR_ALLOW_DATA_LOSS option:

An Alternative to DBCC CHECKDB Command

To overcome the downsides of the DBCC CHEKDB command and repair the corrupt SQL database with complete integrity, you can use a specialized MS SQL repair software, such as Stellar Repair for MS SQL. The software repairs severely corrupt MS SQL database and restores all its components. The SQL recovery software helps reinstate access to the database with minimal manual efforts and time.

Key Features:

Conclusion

If your SQL database is corrupted, you can use the DBCC CHECKDB command to check and repair it. Above, we have explained how to use the DBCC CHECKDB command to repair SQL database. However, using the DBCC CHECKDB command with REPAIR_ALLOW_DATA_LOSS involves risk of data loss. To repair the corrupt database without data loss, you can use a specialized MS SQL repair software such as Stellar Repair for MS SQL. The software helps retrieve all the SQL database components, including tables, deleted table records, indexes, views, etc. You can free download the software to evaluate its functionality and efficacy.

Related Post

Exit mobile version