Set a database to read only mode using SQL Server 2005 and 2008?

   In most DBA or Database Administration purpose we need to set a Database to readonly mode.Here i will show you how you can set Database in Read Only mode for Sql server 2005 and  Sql Server 2008.One thing keep in mind that you can not use same sql command for both sql server 2005 and 2008.Thats why here i will show the different ways to manage a Database to Read Only mode.The another important note is you can not make a database read only until you set the Database in single user mode. So first set the database in single user mode.

To bring a database to the single user mode, use the following query:

[code]
ALTER DATABASE  DATABASENAME SET  SINGLE_USER
[/code]

After execute this query your database will change single user mode.if you want to change single user to multiple user means
take below query

[code]
ALTER DATABASE  DATABASENAME SET  MULTI_USER
[/code]

Sql Server 2005:
   [code]
    EXEC sp_dboption "YourDatabaseName", "read only", "True";
[/code]
   After executing the above command then refresh the your database.
You will see that the DataBase now set to Read Only mode like below:

See Attachements:Image 1


Now if anyone try to enter or update a data into the database he will receive the below error:
See Attachements: image 2

 Sql Server 2008:
To make the Database read only in 2008 run the below SQL:

     USE master;
     GO
     ALTER DATABASE databasename
     SET READ_ONLY;
     GO

No comments:

Post a Comment

Plz Share your comments...