Ever get the following error when restoring a database in SQL Server
Restore failed for server
The Cause
This error occurs when you try to restore a database or log while the database is in use, by any user, including yourself.
The Quick Fix
For my development environment I just took the database offline, closed all open query windows, and put it online. However, you may have a more difficult case in which you need to restrict access. Here is a good code snippet.
USE Master
ALTER DATABASE
GO
RESTORE DATABASE
GO
ALTER DATABASE
GO
Break Down
What this does:
- Uses the master so that your query window is not interacting with
- Sets the "single user" option, with the "rollback immediate" to close any incomplete transactions. This includes users with connections to the database that also need an exclusive lock.
- Performs the restore.
- Sets the database back to multi-user
Hope this helps!
0 comments:
Post a Comment