ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK AFTER 60 --this will give your current connections 60 seconds to complete
--Do Actual Restore
RESTORE DATABASE YourDB
FROM DISK = 'D:\BackUp\YourBaackUpFile.bak'
WITH MOVE 'YourMDFLogicalName' TO 'D:\Data\YourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'D:\Data\YourLDFFile.ldf'
/*If there is no error in statement before database will be in multiuser
mode. If error occurs please execute following command it will convert
database in multi user.*/
ALTER DATABASE YourDB SET MULTI_USER
GO
Use Master
Go
Declare @dbname sysname
Set @dbname = 'databaseName'
Declare @spid int
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname)
While @spid Is Not Null
Begin
Execute ('Kill ' + @spid)
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname) and spid > @spid
End
DECLARE UserCursor CURSOR LOCAL FAST_FORWARD FOR
SELECT
spid
FROM
master.dbo.sysprocesses
WHERE DB_NAME(dbid) = 'dbname'--replace the dbname with your database
DECLARE @spid SMALLINT
DECLARE @SQLCommand VARCHAR(300)
OPEN UserCursor
FETCH NEXT FROM UserCursor INTO
@spid
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQLCommand = 'KILL ' + CAST(@spid AS VARCHAR)
EXECUTE(@SQLCommand)
FETCH NEXT FROM UserCursor INTO
@spid
END
CLOSE UserCursor
DEALLOCATE UserCursor
GO
DECLARE @SPID int = (SELECT TOP 1 SPID FROM sys.sysprocess WHERE dbid = db_id('dbName'))
While @spid Is Not Null
Begin
Execute ('Kill ' + @spid)
Select @spid = top 1 spid from master.dbo.sysprocesses
where dbid = db_id('dbName')
End