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.