How to force a SQL Server 2008 database to go Offline

SqlSql ServerTsqlSql Server-2008

Sql Problem Overview


How do I force my Database to go Offline, without regard to what or who is already using it?

I tried:

ALTER DATABASE database-name SET OFFLINE;

But it's still hanging after 7 min.

I want this because I need to test the scenario.

If it's even possible?

Sql Solutions


Solution 1 - Sql

Go offline

USE master
GO
ALTER DATABASE YourDatabaseName
SET OFFLINE WITH ROLLBACK IMMEDIATE
GO

Go online

USE master
GO
ALTER DATABASE YourDatabaseName
SET ONLINE
GO

Solution 2 - Sql

You need to use WITH ROLLBACK IMMEDIATE to boot other conections out with no regards to what or who is is already using it.

Or use WITH NO_WAIT to not hang and not kill existing connections. See http://www.blackwasp.co.uk/SQLOffline.aspx for details

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionradbyxView Question on Stackoverflow
Solution 1 - SqlabatishchevView Answer on Stackoverflow
Solution 2 - SqlMartin SmithView Answer on Stackoverflow