DBCC CHECKDB Database Corruption Recovery
When database corruption has been discovered, the cleanest way of recovering is to restore your database from a backup and replay the transaction logs up to a point where you know the database to be healthy.
If transaction log backups aren’t regularly taken or if data is constantly changing (or if you just don’t have any full backups that you can go back to), this will not be possible.
Before going any further, take a backup of your database that you can revert back to (just in case something goes catastrophically wrong and leaves you in a worse state than you are already in
There are two options in this circumstance. The first is to run a repair that cannot result in the loss of any data. This is done by running the following T-SQL command (substituting AdventureWorks with the name of your database but keeping the quotes)
DBCC CHECKDB(‘AdventureWorks’, REPAIR_REBUILD)
If this fails (which it will if there are any connections to your database, run this command first to set the database into single user mode. The ROLLBACK IMMEDIATE bit means that any transactions currently in progress will be cancelled and rolled back
ALTER DATABASE AdventureWorks SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
Now run the repair command again. Once complete. Run this to set the database back to Multi User mode
ALTER DATABASE AdventureWorks SET MULTI_USER
In the worst case, this will not be enough to fix your database corruption. Without a backup you can revert to, the only option is to allow SQL to try the repair regardless of whether it needs to lose any data. If you are happy with this then run the command below
DBCC CHECKDB('yourDB', REPAIR_ALLOW_DATA_LOSS)