Database is in Suspect State
There can be many reasons for a SQL Server database can show in suspect mode when you connect to it - such as the device going offline, unavailability of database files, improper shutdown etc. Consider that you have a database named ‘test’ which is in suspect mode
You can bring it online using the following steps:
- Reset the suspect flag
- Set the database to emergency mode so that it becomes read only and not accessible to others
- Check the integrity among all the objects
- Set the database to single user mode
- Repair the errors
- Set the database to multi user mode, so that it can now be accessed by others
Here’s the same code for you to try out
EXEC sp_resetstatus 'test'
ALTER DATABASE test SET EMERGENCY
DBCC CheckDB ('test')
ALTER DATABASE test SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DBCC CheckDB ('test', REPAIR_ALLOW_DATA_LOSS)
ALTER DATABASE test SET MULTI_USER
Database is in Read-Only State
If your database is in read-only mode, you will need to first disconnect all connections to that database before you can bring it online. You can kill the active connections one by one, or just run a command similar to the following
ALTER DATABASE test SET OFFLINE WITH ROLLBACK IMMEDIATE
ALTER DATABASE test SET ONLINE