Created
March 12, 2019 11:25
-
-
Save mattiasnorell/a55ea3f4547578301a12f4b0f3d48b1f to your computer and use it in GitHub Desktop.
Restore MSSQL database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE [Master]; | |
DECLARE @databaseName varchar(8000) = 'DATABASE NAME'; | |
DECLARE @backupPath varchar(8000) = 'c:\path\to\database.bak'; | |
DECLARE @kill varchar(8000) = ''; | |
SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), session_id) + ';' | |
FROM sys.dm_exec_sessions | |
WHERE database_id = db_id(@databaseName) | |
EXEC(@kill); | |
RESTORE DATABASE @databaseName FROM DISK = @backupPath WITH REPLACE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment