Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a SQL Server database's read-only status using T-SQL?

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?

like image 895
Giuseppe Avatar asked May 28 '10 15:05

Giuseppe


People also ask

How do I check SQL database status?

To verify the current state of a database, select the state_desc column in the sys. databases catalog view or the Status property in the DATABASEPROPERTYEX function.

Why is database read only SQL Server?

After moving your SQL Server, the Properties may update to be Read Only. This can result in the Exception similar to "Failed to update database "Acctivate" because the database is read-only". You can update the SQL Database permissions in Microsoft SQL Server Management Studios (SSMS).

How do I get TPS in SQL?

Is there any way to calculate TPS (transaction per second) of SQL Server database? You can use the "Batch Requests/sec." PMON counter (under the SQL Statistics category) for an overall measure of database requests processed per second.

Is T-SQL used in SQL Server?

T-SQL identifiers, meanwhile, are used in all databases, servers, and database objects in SQL Server. These include the following tables, constraints, stored procedures, views, columns and data types.


2 Answers

The information is stored in sys.databases.

SELECT name, is_read_only  FROM sys.databases  WHERE name = 'MyDBNAme' GO  --returns 1 in is_read_only when database is set to read-only mode. 
like image 59
p.campbell Avatar answered Sep 21 '22 12:09

p.campbell


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'); 
like image 24
Masood Hashim Avatar answered Sep 18 '22 12:09

Masood Hashim