Saturday, February 4, 2012

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

Use Master
 Alter Database YOURDB
  SET SINGLE_USER With ROLLBACK IMMEDIATE

  RESTORE DATABASE YOURDB
  FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\YOURDB.bak'

Only use this if the database is not live, and users are not actively performing transactions.
This is a last resort option to be used in cases where all users have logged out, and some unknown service keeps accessing the database.
This will disconnect all other users, and run the restore.
The restore will remove the single_user flag.

1 comment: