SQL Server error 931 occurs when you try to reference a database fragment that is not attached or is in a state that prevents access. The fix is to identify the missing or offline database file, attach it if it exists, or drop the reference if it is no longer needed. This guide walks you through the safe steps to resolve the error without data loss.

What You'll Need

  • Administrative access to the SQL Server instance (sysadmin role).
  • SQL Server Management Studio (SSMS) or another client such as Azure Data Studio.
  • Knowledge of the database name or file path involved in the error message.
  • Access to the file system where the database files (.mdf, .ndf, .ldf) are stored, if you need to reattach.

Step-by-Step

  1. Read the full error message. Note the exact database name and any file paths mentioned. Error 931 often appears alongside other errors (e.g., 5120, 945) that indicate why the fragment is inaccessible.
  2. Check the database state. In SSMS, connect to the instance, expand Databases, and look for the affected database. If it shows as Recovery Pending, Suspect, or Offline, that explains the error. Alternatively, run:
    SELECT name, state_desc FROM sys.databases;
  3. Verify the physical files exist. Using the file paths from the error, check in File Explorer (or your OS file manager) that the .mdf and .ldf files are present and accessible. If they are missing, you will need to restore from backup or attach from another location.
  4. If the database is offline, bring it online. Run:
    ALTER DATABASE [YourDBName] SET ONLINE;
    If this fails, proceed to the next step.
  5. If the database is suspect or recovery pending, attempt emergency mode repair (advanced). Only do this if you have a backup. First set the database to emergency mode:
    ALTER DATABASE [YourDBName] SET EMERGENCY;
    Then run a consistency check:
    DBCC CHECKDB ([YourDBName]);
    Review the output for repair suggestions. If repairs are needed, use:
    ALTER DATABASE [YourDBName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
    DBCC CHECKDB ([YourDBName], REPAIR_ALLOW_DATA_LOSS);
    Warning: REPAIR_ALLOW_DATA_LOSS can delete data. Use only as a last resort and after a backup.
  6. If the files are missing, attach the database from existing files. Use:
    CREATE DATABASE [YourDBName] ON (FILENAME = 'C:\Path\YourDB.mdf'), (FILENAME = 'C:\Path\YourDB_log.ldf') FOR ATTACH;
    If you only have the .mdf, use FOR ATTACH_REBUILD_LOG to recreate the log. Ensure the SQL Server service account has read/write permissions on the folder.
  7. If the database is no longer needed, drop the reference. If the database was detached or deleted but a reference remains (e.g., in a job or query), remove or update that reference. To drop a database that is in a broken state, you may need to set it to emergency mode first, then drop it:
    DROP DATABASE [YourDBName];
    Warning: This deletes the database and its files. Ensure you have a backup.
  8. Restart the SQL Server service if changes do not take effect. In Services.msc, restart the SQL Server (MSSQLSERVER) service. This can clear transient state issues.

Common Mistakes

  • Running REPAIR_ALLOW_DATA_LOSS without a backup. This can cause permanent data loss. Always back up first, even if the database is suspect.
  • Ignoring file permissions. The SQL Server service account needs full control on the database folder. Missing permissions cause attach failures.
  • Assuming the database is gone. Sometimes the files exist but are offline or detached. Check before restoring.
  • Using the wrong file path. A typo in the path in CREATE DATABASE ... FOR ATTACH will fail. Copy the path from the error message.
  • Forgetting to set SINGLE_USER before repair. Other connections can block repair operations.

Tips

  • Always keep regular backups. Error 931 often signals a deeper issue like disk failure or accidental file deletion.
  • Use DBCC CHECKDB regularly to catch corruption early.
  • When attaching, use the WITH MOVE option if you need to relocate files to a different folder.
  • Document database file locations so you can quickly verify their existence.
  • If the database is part of an Availability Group, check the AG health and failover to another replica if possible.

Troubleshooting

If the steps above do not resolve the error, consider these additional checks:

  • Check SQL Server error log. Look for messages around the time of the error for clues about file access or corruption.
  • Verify disk space. A full disk can prevent database files from being accessed or logs from growing.
  • Run DBCC CHECKDB on the master database. Corruption in system tables can cause odd errors.
  • Try attaching a copy of the files on a different SQL Server instance. This isolates whether the issue is with the files or the instance.
  • Contact Microsoft support if the database is critical and repairs fail. Do not attempt further repairs without guidance.

Frequently Asked Questions

What causes SQL Server error 931?

It typically occurs when a database file is missing, offline, or the database is in a suspect state, so SQL Server cannot access the fragment referenced.

Can I fix error 931 without a backup?

Yes, if the files exist and the issue is permissions or offline state. If files are corrupt, a backup is strongly recommended before attempting repairs.

Is it safe to use REPAIR_ALLOW_DATA_LOSS?

Only as a last resort. It can delete data to repair the database. Always back up first and understand the risks.

How do I know which database is affected?

The error message usually includes the database name or file path. Check the SQL Server error log or the output in SSMS.

What if the database files are on a network share?

SQL Server does not support database files on network shares for normal operation. Move the files to local storage or a supported volume.

Can error 931 be caused by a detached database?

Yes, if a query or job references a detached database, you may see this error. Reattach the database or remove the reference.