T-SQL Msg 3101 Exclusive access could not be obtained because the database is in use

Symptom:
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.

Completion time: 2012-10-27T20:46:07.0030490-07:00
Resolution:

Assuming that the database name is TEST_MSCRM and it’s backup file is B:\Backup\TEST_MSCRM.bak’

USE [master]
GO

ALTER DATABASE [TEST_MSCRM]
SET SINGLE_USER WITH ROLLBACK IMMEDIATE; --This rolls back all uncommitted transactions

RESTORE DATABASE [TEST_MSCRM]
FROM DISK = N'B:\Backup\TEST_MSCRM.bak' WITH FILE = 1,
MOVE N'mscrm' TO N'D:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Data\TEST_MSCRM.mdf',
MOVE N'mscrm_log' TO N'D:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Data\TEST_MSCRM.ldf',
NOUNLOAD, REPLACE, STATS = 5;

ALTER DATABASE [TEST_MSCRM] SET MULTI_USER
GO

Leave a Reply

Your email address will not be published. Required fields are marked *