Monday, 20 February 2012

MSSQL: Drop all active connections to a database

In Microsoft SQL Server you can drop all active connections to a database, prior to restoring it for example, by simply taking the database offline and bringing it back online again immediately.

USE master
GO

ALTER DATABASE 
@databaseName
SET OFFLINE WITH ROLLBACK IMMEDIATE
ALTER DATABASE 
@databaseName
SET ONLINE