Skip to content

Instantly share code, notes, and snippets.

@josuebasurto
Created July 24, 2012 16:37
Show Gist options
  • Save josuebasurto/3171082 to your computer and use it in GitHub Desktop.
Save josuebasurto/3171082 to your computer and use it in GitHub Desktop.
Restore a database first making the database single user then rollingback other users transactions, then restoring the backup.
/*
JB 20120724 - MS SQL SERVER 2008 R2
Restore a database first making the database single user then rollingback other users transactions, then restoring the backup.
Notes:
- This script is stored and shared as is.
- Might Work on other versions.
- Need owner privileges over database.
*/
--Get out of wherever you're working on! And let's go masteR!
use master
go
--Single user a database
ALTER DATABASE [ <<Database Name>> ] --Replace varaible for database name
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
--Restore Database
RESTORE DATABASE [ <<Database Name>> ] --Replace varaible for database name
FROM DISK = N'<<Bak Filepath>>' --Replace fot database filepath
WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment