Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server Error 589: Fix Data Access Attempts

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

SQL Server error 589, "This statement has attempted to access data," occurs when a statement tries to read or write data while the database is in a state that does not allow it—most commonly during a restore, when the database is in single-user mode, or when the database is read-only. The fix depends on the exact cause, but usually you need to either complete the restore, change the database state, or ensure the statement runs in a context that permits data access. Below are the precise steps to diagnose and resolve each scenario.

What You'll Need

  • SQL Server Management Studio (SSMS) or another query tool with administrative privileges on the SQL Server instance.
  • Permissions to alter database state (typically sysadmin or db_owner for the affected database).
  • If the database is being restored, access to the backup file or the restore operation that is in progress.

Step-by-Step

  1. Identify the exact error context. The full error message usually includes the database name and the statement that failed. Run the following query to check the state of all databases:
    SELECT name, state_desc, is_read_only, is_in_standby FROM sys.databases;
    Note the state_desc column. If it says RESTORING, the database is mid-restore. If it says ONLINE but is_read_only is 1, the database is read-only. If it says SINGLE_USER, only one connection is allowed.
  2. If the database is in RESTORING state: A restore operation is in progress or was interrupted. You must either wait for it to finish or cancel it. To cancel a restore, run:
    RESTORE DATABASE [YourDatabase] WITH RECOVERY;
    This completes the restore and brings the database online. If the restore was from a backup that is not fully valid, you may need to restore from a different backup. If you do not have a valid backup, you cannot force the database online without risking corruption—do not attempt to set it online by other means.
  3. If the database is read-only: To make it writable, run:
    ALTER DATABASE [YourDatabase] SET READ_WRITE;
    This requires the database to be online and you must have permission. If the database is a secondary replica in an Always On availability group, you cannot change it to read-write on the secondary—you must connect to the primary replica.
  4. If the database is in single-user mode: This allows only one connection. If that connection is not your session, your statement will fail. To force single-user mode off, first ensure no other connections are active, then run:
    ALTER DATABASE [YourDatabase] SET MULTI_USER;
    If you cannot connect because the database is in single-user and another session is holding it, use the WITH ROLLBACK IMMEDIATE option to terminate other connections:
    ALTER DATABASE [YourDatabase] SET MULTI_USER WITH ROLLBACK IMMEDIATE;
    This will roll back any uncommitted transactions in those sessions—be aware that this may cause data loss for those transactions.
  5. If the database is OFFLINE or EMERGENCY: These states also block data access. To bring it online, run:
    ALTER DATABASE [YourDatabase] SET ONLINE;
    If the database is in EMERGENCY mode, you may need to repair it first (see Troubleshooting).
  6. If the error occurs inside a transaction or trigger: Some statements, like INSERT or UPDATE inside a trigger, may attempt to access data that is not yet committed. Check if the statement is in a trigger that fires on a table that is being modified. In that case, the error may be due to the trigger trying to read the table while it is locked. Solution: use WITH (NOLOCK) or SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED if appropriate, but be careful about dirty reads. Better: restructure the trigger to avoid accessing the same table.
  7. If the error occurs during a backup or restore command: For example, RESTORE DATABASE with REPLACE might fail if the database is in use. Ensure no other connections are active. You can use ALTER DATABASE [YourDatabase] SET SINGLE_USER WITH ROLLBACK IMMEDIATE to force all other connections to close, then run the restore, then set it back to multi-user.
  8. After making any changes, test the original statement again. Run a simple query like SELECT * FROM sys.tables to confirm the database is accessible. If the error persists, re-check the database state with the query from step 1.

Common Mistakes

  • Assuming the database is online when it is not. Always check sys.databases first—do not guess.
  • Using WITH ROLLBACK IMMEDIATE without understanding the consequences. This can terminate active transactions and cause data loss. Use it only when you are sure no critical work is in progress.
  • Forgetting that a database in single-user mode may be held by your own connection. If you are connected to the database, you are the single user, so the error is not from that—check for other sessions.
  • Attempting to set a database to READ_WRITE when it is part of an availability group. You must do this on the primary replica, not the secondary.
  • Ignoring the possibility of a corrupted database. If the database is in SUSPECT or EMERGENCY mode, simply setting it online will not work; you need to repair or restore.

Tips

  • Use the sys.databases view as your first diagnostic tool—it gives you the current state, read-only flag, and whether the database is in standby.
  • If you are restoring a database regularly, consider using RESTORE WITH RECOVERY at the end of your restore sequence to bring it online automatically.
  • For development environments, you can set a database to READ_COMMITTED_SNAPSHOT to reduce blocking, but this does not fix error 589 if the database is offline.
  • Always script your restore operations to include error handling and a final WITH RECOVERY to avoid leaving the database in a restoring state.
  • Check the SQL Server error log for more details around the time of the error—it may show a backup or restore job that failed.

Troubleshooting

If the database is in SUSPECT state: This means SQL Server cannot start the database. Do not try to force it online. Instead, check the error log for the cause (often a missing file or disk error). You may need to restore from a backup. If you have no backup, you can try to set the database to EMERGENCY mode and run DBCC CHECKDB to attempt repair, but this is risky and may lead to data loss. Always have a backup before attempting repair.

If the error occurs during a backup: The backup statement itself is trying to read data, and if the database is in a state that blocks reads (like OFFLINE), it will fail. Ensure the database is online. If the backup is failing because the database is in use, use BACKUP DATABASE ... WITH COPY_ONLY? No, that does not help—use BACKUP DATABASE ... WITH INIT and ensure no other operations are running.

If the error occurs in a stored procedure or function: The procedure might be trying to access a table that does not exist or is inaccessible. Check the permissions on the objects. Also, if the procedure is a CLR stored procedure, ensure the assembly is not in an invalid state.

If the error occurs after a failover in an availability group: The secondary replica is read-only, and any write statement will fail with a different error (usually 3906). Error 589 might appear if the secondary is not readable. Check sys.dm_hadr_database_replica_states to see the state.

If none of the above helps: Restart the SQL Server service (if you have permission) to clear any stuck states. Before restarting, ensure you have a current backup. After restart, check the database state again.

Frequently Asked Questions

Can I ignore error 589 and continue working?

No. The statement that triggered it did not execute. You must resolve the underlying database state before you can proceed.

Will setting the database to MULTI_USER with ROLLBACK IMMEDIATE cause data loss?

Yes, it can. Any uncommitted transactions in the other sessions will be rolled back. Only use this when you are certain no important work is in progress.

How do I know if a restore is still running?

Use SELECT session_id, command, percent_complete FROM sys.dm_exec_requests WHERE command LIKE '%RESTORE%';. If you see a row, the restore is in progress.

Is error 589 the same as error 3906?

No. Error 3906 is specifically for failed updates on a read-only database. Error 589 is more general and can occur in various states.

Can I prevent this error from happening?

Yes, by ensuring databases are properly brought online after restores, avoiding leaving databases in single-user mode, and monitoring database states with alerts.

What if I don't have permission to alter the database?

Contact your SQL Server administrator. You cannot resolve this error without the necessary permissions.