How do you take an SQL Server database offline and then back online?

Posted: (EET/GMT+2)

 

I was today working on an old SQL Server installation, and needed to put one of the databases offline. This was SQL Server version 2005, which meant that the useful "Take Offline" and "Bring Online" management options are not available in SQL Server Management Studio (for that version of the tools). Since I didn't want to install anything more on the old server, I decided to set the database offline using an SQL command.

Here's how to take a database offline:

ALTER DATABASE MyDataBase SET OFFLINE WITH ROLLBACK AFTER 30 SECONDS

This will take the database "MyDataBase" offline after latest 30 seconds, if there are open transactions. You can execute this command using for instance SQL Server Management Studio or SWLCMD if you have appropriate rights to the database.

Once your database is offline, no one can log into it, or read to change its contents.

To bring a database back online, run this command:

ALTER DATABASE MyDataBase SET ONLINE

A reference of the ALTER DATABASE T-SQL command can be found here on MSDN.

Happy hacking!