Taking a SQL Server Database offline

I’ve had to take a SQL Server database offline several times lately, and I keep forgetting the names of the commands I need to use, so I’m saving them here for future use.

To take the database offline:

ALTER DATABASE database_name SET OFFLINE

To put the database back online:

ALTER DATABASE database_name SET ONLINE

Sometimes taking the database offline will fail, because there are existing connections to the database. You can find those existing connections with sp_who():

EXEC sp_who

By searching for your database in the dbname column.

Then, assuming you have permission to kill those connections, you can use spid from the same row to do so:

KILL spid

Once all those connections are gone, you should be able to take the database offline.

Advertisements
This entry was posted in Database, Miscellaneous. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s