SQL Server error 942 means the database you are trying to use is not available to the instance you are connected to. The most common cause is that the database has been taken offline, is in the middle of a restore, or was detached and never reattached. The fix is almost always to bring the database back online with ALTER DATABASE ... SET ONLINE, or to reattach it if it was detached. Before you do anything, confirm you are connected to the correct SQL Server instance and that you have permission to alter the database.

What You'll Need

  • Access to SQL Server Management Studio (SSMS) or another client such as Azure Data Studio, connected to the same instance that hosts the database.
  • An account with ALTER ANY DATABASE permission, or membership in the dbcreator or sysadmin fixed server role.
  • The database name exactly as it appears in sys.databases.
  • If the database was detached, the original .mdf file (and .ldf file if you have it) and the folder path where they live.
  • If the database is mid-restore, the backup file or the RESTORE statement you intend to finish.

Step-by-Step

  1. Confirm the instance and database name. In SSMS, check the server name in the Object Explorer toolbar. Then run SELECT name, state_desc FROM sys.databases; in a new query window. The state_desc column tells you why the database cannot be opened: OFFLINE, RESTORING, RECOVERY_PENDING, SUSPECT, or ONLINE if the name simply does not exist on this instance.
  2. If the state is OFFLINE, bring it online. Run ALTER DATABASE [YourDB] SET ONLINE; replacing YourDB with the real name. This is the direct fix for error 942 when the database was taken offline intentionally or by a failed operation.
  3. If the state is RESTORING, finish or cancel the restore. A database stays in RESTORING when a restore used WITH NORECOVERY and no final WITH RECOVERY was issued. Run RESTORE DATABASE [YourDB] WITH RECOVERY; to complete it, or restore the remaining log backups first if you still need them.
  4. If the state is RECOVERY_PENDING, check the SQL Server error log. This usually means the files are present but SQL Server could not open them, often because of permissions or a missing file. Open the error log in SSMS under Management > SQL Server Logs, or query sys.dm_server_services to confirm the service account, then verify that account has read and write access to the .mdf and .ldf files.
  5. If the state is SUSPECT, do not force it online blindly. Run DBCC CHECKDB ([YourDB]) only after you have a current backup. If CHECKDB reports corruption, restore from backup rather than attempting repairs on a production database without guidance.
  6. If the database does not appear in sys.databases at all, it was likely detached. Reattach it with CREATE DATABASE [YourDB] ON (FILENAME = 'C:\Path\YourDB.mdf'), (FILENAME = 'C:\Path\YourDB_log.ldf') FOR ATTACH; using the real file paths. If the log file is missing, use FOR ATTACH_REBUILD_LOG instead, which rebuilds the log and requires the database to be cleanly shut down.
  7. Verify the fix. Re-run SELECT name, state_desc FROM sys.databases WHERE name = 'YourDB'; and confirm the state is ONLINE. Then try your original query or connection again.

Common Mistakes

  • Running the fix on the wrong instance. Error 942 is per-instance; a database that is online on one server can be offline or absent on another.
  • Typing the database name without brackets when it contains spaces or reserved words, which causes a syntax error instead of fixing the problem.
  • Using FOR ATTACH_REBUILD_LOG on a database that was not cleanly shut down; this can fail or leave the database inconsistent. Use it only when the log is genuinely missing and you have a backup.
  • Forcing a SUSPECT database online with emergency-mode tricks before taking a backup, which can make data loss worse.
  • Assuming error 942 means the files are corrupt. In most cases the files are fine and the database is simply offline, restoring, or detached.
  • Ignoring permissions on the data folder after moving files between servers or drives.

Tips

  • Always take a backup before any ALTER DATABASE, ATTACH, or DBCC operation on a database you care about.
  • Record the output of SELECT name, state_desc, user_access_desc FROM sys.databases; before and after your fix so you can see exactly what changed.
  • If a database is intentionally kept offline, document it so the next person does not treat error 942 as an emergency.
  • When moving a database between servers, detach and attach rather than copying files while the database is online.
  • Check the SQL Server error log first for RECOVERY_PENDING and SUSPECT states; it usually names the file or permission that failed.

Troubleshooting

If ALTER DATABASE ... SET ONLINE fails, read the accompanying error message carefully. A file-not-found error means the .mdf or .ldf path in sys.master_files no longer matches the disk; correct the path with ALTER DATABASE ... MODIFY FILE and try again. An access-denied error points to the SQL Server service account lacking permissions on the folder, which you fix in Windows file properties, not in SQL Server.

If the database is RESTORING and you do not have the remaining backups, you cannot complete the restore; you will need to restore from the last full backup and accept the data loss up to that point. If the database is SUSPECT and CHECKDB cannot run, restore from backup. If you cannot connect to the instance at all, the problem is connectivity or authentication, not error 942, so check the service is running and your login is valid before continuing.

Frequently Asked Questions

What does SQL Server error 942 mean exactly?

It means the database cannot be opened because it is not in an online, usable state on the instance you are connected to, typically because it is offline, restoring, recovery pending, suspect, or detached.

Can error 942 be caused by connecting to the wrong server?

Yes. If the database exists on a different instance, the current instance has no such database, and the same error appears. Confirm the server name in your connection first.

Will bringing the database online lose data?

No, SET ONLINE does not change data. It only changes the database state. Data loss risk comes from restore, attach, or repair operations, which is why a backup first is recommended.

What is the difference between OFFLINE and RESTORING?

OFFLINE means the database was explicitly taken offline and can be brought online directly. RESTORING means a restore is incomplete and must be finished with WITH RECOVERY or by restoring remaining backups.

Can I fix error 942 without sysadmin rights?

Only if your account has ALTER ANY DATABASE or is in the dbcreator role. Otherwise ask a sysadmin to run the ALTER DATABASE statement for you.

Does error 942 mean the database files are corrupt?

Not usually. Corruption shows up as SUSPECT or through DBCC CHECKDB errors. Error 942 is about state, not file integrity, in the majority of cases.