Thursday, July 26, 2012

Recovering a SQL Server Database From SUSPECT Mode


How to Repair Database which is in SUSPECT mode in SQL Server 2005/2008



Morning you come to your office and you see your one of the critical database in SUSPECT mode and you are not able to do any action against that database. You need to repair the database at the earliest.



Why Database Is In SUSPECT Mode



There could be so many reasons for this but here are the main reasons that I can think of.

1.    Data files or log files are corrupt.
      2.    Database server was shut down improperly.
      3.    SQL cannot complete a rollback or rollforward operation.

If you want to find the exact reason why your database is in SUSPECT mode, you can use below query. This should give you the exact error message.

DBCC CHECKDB ('<Database Name>') WITH NO_INFOMSGS, ALL_ERRORMSGS


How to Repair the database and bring it online

Follow below steps to bring the database ONLINE and accessible.


1.    Change the status of your database by running following query

          EXEC sp_resetstatus '<Database Name>';

2.    Set the database in EMERGENCY mode by running following query

ALTER DATABASE <Database Name> SET EMERGENCY;

3.    Check the database for any inconsistency by executing below query

DBCC CHECKDB('<Database Name>');

4.    If you get any error after executing DBCC CHECKDB then immediately bring the database in SINGLE USER MODE by running following query. If no error found then you are done.

          ALTER DATABASE <Database Name> SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

5.    Take the backup of the database just to be in safe side.

6.    Run the following query as next step. Remember while using the below query, that uses REPAIR_ALLOW_DATA_LOSS, is a one way operation that is once the database is repaired all the actions performed by these queries can’t be undone. There is no way to go back to the previous state of the database. So as a precautionary step you should take backup of your database in step 5 mentioned above.

DBCC CHECKDB ('<Database Name>', REPAIR_ALLOW_DATA_LOSS);

7.    Finally bring the database in MULTI USER mode by running following query.

ALTER DATABASE <Database Name> SET MULTI_USER;

8.    At last step you verify the connectivity from your database. Now users should be able to connect to the database properly. If any data loss, you may need to get from the backup taken in step 5.

Now your database is ONLINE and accessible. Now Rock and have the coffee to RELAX.






10 comments:

  1. You must install the free DEMO version of SysTools SQL Database Recovery software for recovering a SQL server database from suspect mode. After checking with “DBCC CHECKDB” command, you are not able to fix SQL database error, then you can use this MDF File Repair software and the software ensures to get back 100% SQL database recovery with all components.

    ReplyDelete
  2. I found an application which you can try at free of cost to repair corrupt sql database and also to see preview of recovered sql data. Here is the link to download the software and use the free trial version.
    http://www.recoverfilesdata.com/sql-database-mdf-recovery.html

    ReplyDelete
  3. How to take backup when the db is in emergency mode?

    ReplyDelete
    Replies
    1. You should backup your database after changing to single user mode. This is just to make sure that when we run DBCC with data loss option, we will have the backup available.

      Delete
  4. I have repaired the sql mdf file by using the sql database recovery tool and this is very helpful for me. The mdf file successfully repaired via this tool.

    ReplyDelete
  5. Hi Ranjeet,

    DBCC CHECKDB with repair_allow_data_loss is not the last resord to recover data from suspect SQL server database. There is a 3rd party tool that successfully repairs & recovers data from suspect database.

    http://www.mssqldatabaserecovery.com/

    You need to perform following actions after making database online:

    1.Run DBCC CHECKDB again and see if it comes out clean without any error.That is important so as to confirm there is no inconsistency in database
    2.Please try to find out what caused this issue was it disk ,was it bad memory board,or something else.Refer to event viewer when this issue first occurred also refer to errorlogs of SQL Server.
    3 If checkdb comes out clean immediately take full backup of the database and keep it some where safe.Make sure you put proper strategy for baking up databases.
    4 Query various tables of your database to see if any data loss has occurred.

    ReplyDelete
  6. Another SQL Database Recovery application and retrieve MDF and NDF file from SQL server database. You can repair MDF files along with entire database such as tables, triggers, stored procedures, functions, views etc. Read more:- http://www.softmagnat.com/sql-database-recovery.html

    ReplyDelete
  7. Use SQL Server Recovery tool which can estore all the SQL file objects such as tables, views, triggers, stored procedures, constraints, default constraints, indexes, rules, user defined functions & user defined data types.

    See at:- http://www.mannatsoftware.com/stellar-phoenix-sql-server-recovery.html


    ReplyDelete
  8. Try SQL Database Repair is an apt utility and advanced recovery software as it can recover damaged or corrupt SQL Server files. It thoroughly repairs MDF database files which are severely corrupt and retrieves back SQL Database files. Accidental deletion, invalid file header, inaccessible partitions and drivers, insufficient space, media corruptions, virus attacks etc. are the threats to corrupt SQL Server database.

    ReplyDelete