This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing the script to make a database read-only or changing from read-only to the read-write mode of SQL Server.
When we are attaching MDF or LDF files, sometimes the database status shows that the database is in Read-Only mode.
If we have some historical archive databases, we can also set the Read-Only mode to that database so that any database user can only read the database.
Script to make database Read-Only:
1 |
ALTER DATABASE [DatabaseName] SET READ_ONLY WITH NO_WAIT |
Script to make database Read-Write:
1 |
ALTER DATABASE [DatabaseName] SET READ_WRITE WITH NO_WAIT |
If you get any error like that database is already in use, you can alter database for a single user which closes all existing connections.
You can use below script for converting database into Single User Mode:
1 |
ALTER DATABASE [DatabaseName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; |