SqlBak Blog

Failed to update database because the database is read-only

Here we will discuss how to solve the error message “Failed to update database because the database is read-only” that you can face with working with SQL Server. Below is the complete text of this error:

Msg 3906, Level 16, State 1, Line 10
Failed to update database "MyData" because the database is read-only.

Of course, an experienced DBA can say that your database is read-only, and you don’t have any way to write into it. All you need is simply mark the database as read-write. That’s it. But still, we are going to show you, how you can solve this issue using simple T-SQL Commands or SQL Server Management Studio.

Failed to update database because the database is read-only – Reproducing

  • Create a database with one table
    CREATE DATABASE MyData CONTAINMENT = NONE
    ON PRIMARY
    ( NAME = N'MyData', FILENAME = N'D:\Backups\MyData.mdf' , SIZE = 5120KB , FILEGROWTH = 1024KB )
    LOG ON
    ( NAME = N'MyData_log', FILENAME = N'D:\Backups\MyData_log.ldf' , SIZE = 2048KB , FILEGROWTH = 10%)
    GO
    USE MyData
    GO
    CREATE TABLE tbl_Table (id INT)
    GO
  • Mark “MyData” database as Read Only
    USE master
    GO
    ALTER DATABASE MyData SET READ_ONLY
    GO
  • Insert any value into the table
    USE MyData
    GO
    INSERT INTO tbl_Table VALUES (1), (2)
    GO

After you have executed “INSERT INTO” operation, you will receive the following error message:

Msg 3906, Level 16, State 1, Line 10 
Failed to update database "MyData" because the database is read-only.

Failed to update database because the database is read-only – Solution

We successfully reproduced the error, now let’s solve it using T-SQL Commands

All we need to do is to mark the database “MyData” as read-write. We can simply do it using the next T-SQL Commands:

USE master
GO
ALTER DATABASE MyData SET READ_WRITE WITH NO_WAIT
GO

Now, database “MyData” is marked as “read-write”. To ensure the issue is solved, insert any value into the table.

USE MyData
GO
INSERT INTO tbl_Table VALUES (1), (2)
GO
SQL query checking if the database is in read-only state
Checking if the database is in read-only state using an SQL query

That’s it! All 2 rows were affected successfully!

Using SQL Server Management Studio

The another way to solve “Msg 3906, Level 16, State 1, Line 10 Failed to update database because the database is read-only” is to use SQL Server Management Studio (SSMS). Follow these simple steps and mark your database “read-write”.

  1. Right-click the database you need to mark as read-write
  2. Select “Properties”
  3. In the “Database Properties” window select the “Options” tab
  4. Scroll down and find the “State” tab, go to the “Database Read-Only” statement and select “True”

    Changing database to read-write in SSMS
    Steps to change a database from read-only to read-write using SSMS

7 thoughts on “Failed to update database because the database is read-only”

  1. Following SQL shows your current permissions against a database:

    use MyData;
    go

    SELECT *
    FROM fn_my_permissions(null, ‘DATABASE’)
    ORDER BY subentity_name, permission_name ;

    The above shows me I have UPDATE permission on the database.

    Following SQL shows your current permissions against the table in that database:

    use MyData;
    go

    SELECT *
    FROM fn_my_permissions(‘MyData.dbo.tbl_Table’, ‘OBJECT’)
    ORDER BY subentity_name, permission_name ;

    The above shows me I have UPDATE permission on that table.

    Despite having UPDATE permission against both the database and table, when I perform an update I get the error of this article:

    Msg 3906, Level 16, State 1, Line 10
    Failed to update database “MyData” because the database is read-only.

    This simply shows that this database-level setting overrides individual permissions, and that the function fn_my_permissions can give you results suggesting you have permission to UPDATE, yet you can still be prevented from updating. Interesting.

    Reply
  2. In my case Not Working, the database ReadOnly = False and my Excel with VBA is with the same message . Database or Object Read Only, couldn’t update..

    Reply
  3. Just wondering what one would do if they had the same error in a SQL Server 2016 Enterprise AlwaysOn Availability Group? When the primary fails over to a secondary replica, I get the failed to update database error when writing to the database through an ODBC connection.

    Reply
    • We have encountered same issue, and SQL Server 2016 Enterprise AlwaysOn under sp2 product version: 13.0.5026.0.
      Whether update to latest CU could resolve the issue?

      Reply
  4. I’m getting my error because of a log in issue.

    I can log in and see that database, but I can’t access the database because it is read-only.

    Reply

Leave a Comment