733
Managing SQL databases can be a tricky business, especially when it comes to ensuring data availability and access. There are times when we need to take a database offline or bring it back online for various reasons, such as moving it to a new server or checking if it’s no longer in use. That’s where the “scream test” comes in handy! In this blog post, let’s use a super simple T-Sql to switch your SQL database between offline and online modes. Just by knowing these simple scripts, you’ll be able to handle your database management tasks like a pro!
T-Sql
--Creat a test database
CREATE DATABASE [MyTestDb]
GO
--Set the database offline
ALTER DATABASE [MyTestDb] SET OFFLINE
WITH ROLLBACK IMMEDIATE
GO
--Set the database online
ALTER DATABASE [MyTestDb] SET ONLINE
WITH ROLLBACK IMMEDIATE
GO
Heads up!
On certain occasions, you might not be able to set the database to an offline state right away due to reasons such as the database still being in use. In these situations, it's necessary to first set the database to "read only" before executing the offline script.
To learn how to set the database to read-only, click here.
Note
For further information on this post, kindly refer to the original source provided in the links below.
Source: sqlauthority, mssqltips