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

    技术2022-06-28  57

    There are times where you may need to restore a database, but receive the error "Exclusive access could not be obtained because the database is in use".  This is usually due to a process using the database.  There are a number of ways to work around this occurance.  If you have a complete backup which you are trying to use, I have found the following to work best:

    USE Master ALTER DATABASE dbname SET SINGLE_USER WITH ROLLBACK IMMEDIATE RESTORE DATABASE dbname FROM DISK = 'C:/Program Files/Microsoft SQL Server/MSSQL/Backup/dbbackupfile.bak' WITH REPLACE

    After the restore completes successfully, you can ensure multi-user mode is reenabled by executing the following query:

    ALTER DATABASE dbname SET MULTI_USER WITH ROLLBACK IMMEDIATE GO

    Hope someone finds this helpful.


    最新回复(0)