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).
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.
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.
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.
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.
Try following steps.
services.msc
console.Run following command in query analyzer
ALTER DATABASE `YOURDATABASE_NAME`
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
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.
Set the database mode to multi user again
ALTER DATABASE `YOURDATABASE_NAME` SET MULTI_USER
Hope this helps.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With