I need to know how to interrogate a Microsoft SQL Server, to see if a given database has been set to Read-Only or not.
Is that possible, using T-SQL?
Querying sys.databases for checking a DB's Read-Only property will only give the right information if the database has been explicitly set to Read-Only mode.
For databases that are in the passive servers (e.g. in AlwaysOn technology Secondary Servers), even though the databases cannot be written into, their Read-Only mode in sys.databases would still be set as False(0).
Hence, it is advisable to check the Read-Only mode of databases using the statement:
SELECT DATABASEPROPERTYEX('MyDBNAme', 'Updateability');
SELECT @@SERVERNAME AS ServerName, DATABASEPROPERTYEX('(databasename)', 'Updateability') AS Status;I was trying to use the p.campbell's answer to check if my Azure SQL DB is the primary one or the read only replica - it didn't work. Both the primary DB and the replica returned had 0 on the is_read_only field.
Here's what worked for me:
SELECT DATABASEPROPERTYEX('MyDBNAme', 'Updateability');
the above select statement returns string 'READ_ONLY' or 'READ_WRITE'.
Here is a command to display or set this property.
EXEC sp_dboption "AdventureWorks", "read only"
Sample output
OptionName CurrentSetting
read only OFF