I was struck with a SQL Server database which is on suspect mode and need to recover it without having any recent backups in hand. I Googled for quite a long time and found these steps to bring back the database to online state without losing any data. I found these steps very useful and I hope they will be useful to others who face similar problem
Step 1:
Open SQL Server Management Studio and connect to the SQL Server. Make sure that your current database is set to master
Step 2:
Clear the suspect mode of the database using sp_resetstatus DatabaseName. This will clear the suspect flag and make the database available online
Step 3:
Change the database status to Emergency using the following command. Emergency mode allows you to access the databases as normal but with no consistency guarantee. This option also allows us to export the table data so that we can minimize the damage.
ALTER DATABASE DatabaseName SET EMERGENCY;
Step 4:
Restrict database to single user by changing the access mode as mentioned below
ALTER DATABASE DatabaseName SET SINGLE_USER;
Step 5:
Run the CHECKDB command with “REPAIR_ALLOW_DATA_LOSS” option. This option should be tried as last option as it always behaves the way it is named. We are not sure of what data it removes.
DBCC CHECKDB (DatabaseName, REPAIR_ALLOW_DATA_LOSS) WITH NO_INFOMSGS, ALL_ERRORMSGS;
Even though the step 4 is expected to result in data loss while repairing the database, I was lucky to restore the database without any loss. My job would have been much easier if the admin of the database server followed few general guidelines that allow recovery of database in case of any unexpected failures. Here are the few of them
- Backup your data frequently. (once in two days or daily or even more frequent depending on your needs)
- Have multiple backups. Move the backups to external drives or tapes frequently
- Validate that your backups are good by performing trial restores to alternate server
- Run CHECKDB regularly if possibly in your case, to know how long it regularly takes
Thanks for sharing.
As DBA, I won’t suggest using Repair Allow Data Loss option because you may or may not lose your data (clears from the name).
Yes, If you have updated backup then, you can restore the database from that backup copy and, it is the safest way to save your data. Few users also reported in the forum about database offline status and they are unable to attach the database. In both cases, MDF file corruption could be a reason. I also suggest maintaining a proper backup plan to handle these types of situations.
When Manuel methods don’t work then, you can go for third party SQL repair software. I have used one software (Stellar Repair for MS SQL) to repair production database MDF file. As the data file was big and backup copy was not updated, that’s why we choose it. All well that ends well. We repaired our database successfully.
Dear Anish
If i am correct you are uisng MYSQL and repaired the table using the syntax “REPAIR TABLE TableName”
SQL Server also has similar options to recover a table alone.One such command is
DBCC CHECKTABLE
Last week one of mt blogs wp_posts table crashed, used the repair function and fixed it.