1.8K
Managing your database access levels can be a crucial aspect of ensuring smooth operations and data integrity, and T-SQL is your key to achieving this. There may be situations where you’ll need to set your SQL database to read only, such as when updates are unnecessary or the database serves as an archive. In this blog post, let’s dive into the simple yet important process of using T-SQL to switch between read only and read/write access for your SQL databases. So, let’s get started and enhance our database management skills with T-SQL!
T-Sql
--Creat a test database
CREATE DATABASE [MyTestDb]
GO
--Set database to read_only
ALTER DATABASE [MyTestDb] SET READ_ONLY
WITH ROLLBACK IMMEDIATE
GO
--Set database to read/write
ALTER DATABASE [MyTestDb] SET READ_WRITE
WITH ROLLBACK IMMEDIATE
GO
Heads up!
If you see an error saying the database is still in use, you can fix it by changing the database to SINGLE USER mode.
To learn how to set the database to SINGLE USER or MULTI-USER mode, click here.
Note
For further information on this post, kindly refer to the original source provided in the link below.
Source: sqlauthority
