Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a SQL Server 2008 database to go Offline

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?

like image 322
radbyx Avatar asked Jun 09 '10 12:06

radbyx


People also ask

How do I force a SQL database offline?

Login to SQL Server Management Studio. In the Object Explorer, select the database you want to take offline and right-click. In the right-click menu go to Tasks >> Take Offline. In the pop-up window, choose the check box under the Drop All Active Connections and click OK.

How do I force close a connection in SQL Server?

Right-click on a database in SSMS and choose delete. In the dialog, check the checkbox for "Close existing connections." Click the Script button at the top of the dialog.

Can I run SQL offline?

Microsoft SQL Server can run on a network, or it can function without a network.

How temporarily disable SQL Server?

Using SQL Server Management StudioRight-click SQL Server Agent, and then select either Start, Stop, or Restart. In the User Account Control dialog box, click Yes.


2 Answers

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 
like image 180
abatishchev Avatar answered Sep 28 '22 14:09

abatishchev


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

like image 39
Martin Smith Avatar answered Sep 28 '22 15:09

Martin Smith