Exclusive access could not be obtained because the database is in use_OFFLINE_ONLINE

Usually I used to get this type error while i am trying to RESTORE databases. So to avoid these type of errors permanently we need to write RESTORE command in between changing SINGLE_USER and MULTI USER statements. See below command.

Msg 3101, Level 16, State 1, Line 2
Exclusive access could not be obtained because the database is in use.
Msg 3013, Level 16, State 1, Line 2
RESTORE DATABASE is terminating abnormally.
Msg 3117, Level 16, State 1, Line 3
The log or differential backup cannot be restored because no files are ready to roll forward.
Msg 3013, Level 16, State 1, Line 3
RESTORE DATABASE is terminating abnormally.

Don't use GO command between the below statement run it as a single script.
USE [master]
ALTER DATABASE [Adventureworks] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
---RestoreCommand between these two statements.
RESTORE DATABASE Adventureworks FROM DISK = 'D:\Adventureworks_full.bak' 
WITH NORECOVERY
ALTER DATABASE Adventureworks SET MULTI_USER
USE [master]
ALTER DATABASE DatabaseNameHere
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE DatabaseNameHere
SET OFFLINE

Comments

Popular posts from this blog

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

Pre login Handshake or Connection Timeout Period

Transparent Data Encryption(TDE) with Master Key and Certificate in SQL Server