Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database is not accessible after 'taking it offline' process failed

I was trying to detach the DB when it gave me an error that it is currently in use. Hence, I tried to take my DB offline but it failed saying

'an exception occured while executing a transact SQL statement or batch 

-> ALTER DATABASE failed because a lock could not be placed on database 'myDB'. Try again later.
    ALTER DATABASE statement failed. (Microsoft SQL Server, Error: 5061)'

Now if I try and access the DB it says it is not accessible. What can I do to make my DB accessible again?

My aim was to detach the DB, relocate its secondary database file to a new drive and reattach it (simply because of space issues).

like image 520
Vaibhav Avatar asked Jul 09 '12 06:07

Vaibhav


People also ask

How do I get my SQL database back online?

Bringing Database Online Using SSMSLogin to SQL Server Management Studio. In the Object Explorer, right-click the offline database. In the right-click menu select Tasks >> Bring Online. On selecting Bring Online, a popup window will open and display the status of the action.

What does taking a DB offline do?

Taking the database offline is a quick way to stop the bleeding until you get the bug identified. You can then bring it back online to assess the damage within the database.

Can you take a database offline?

Steps to Take MS SQL Database Offline Expand Databases option and locate the database that you wish to take offline. Right-click the database and select Tasks >> Take Offline.

Do I need to take database offline to restore?

It is RECOMMENDED that the SQL database that you will restore "over" is set to the Offline status in SQL Server settings, otherwise the restore from NovaBACKUP could fail if it that SQL database is actively being written to and/or accessed by users.


2 Answers

Try following steps.

  1. Restart the SQL server service using services.msc console.
  2. Now connect to your server using SQL Server Management Studio.
  3. Run following command in query analyzer

      ALTER DATABASE `YOURDATABASE_NAME`  
      SET SINGLE_USER WITH ROLLBACK IMMEDIATE 
    
  4. Now Right-click the database name, point to Tasks, and then click Detach. The Detach Database dialog box appears

OR 5. Run your command to Relocate the secondary database.

  1. Set the database mode to multi user again

    ALTER DATABASE `YOURDATABASE_NAME` SET MULTI_USER 
    

Hope this helps.

like image 125
Furqan Hameedi Avatar answered Sep 23 '22 00:09

Furqan Hameedi


As an alternative to step one in Furqan's answer, you might not need to restart the SQL Server instance, but only the SQL Server Management Studio instance, which was used to initiate the "Take Offline" task.

like image 37
Magnus Kragelund Avatar answered Sep 24 '22 00:09

Magnus Kragelund