Query to Repair Suspect Database In SQL Server

This is the query to repair suspect database in SQL Server.  

If your Database is marked as suspected, here are the steps to fix it.

In this you have replace “dbName” with suspected db name and run this query in master database.

Step 1 

--command to recover suspected database

ALTER DATABASE DbName SET EMERGENCY

DBCC checkdb('DbName')

ALTER DATABASE DbName SET SINGLE_USER WITH ROLLBACK IMMEDIATE

DBCC CheckDB ('DbName', REPAIR_ALLOW_DATA_LOSS)

ALTER DATABASE DbName SET MULTI_USER

This step 1 cannot set Index created for that database so now we have to rebuild the index. This query should be run in that suspected database.

Step 2

-- command to rebuild all indexes

EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)"