Troubleshooting Guide: SQL Server Error 7939 — “Cannot detach database because it does not exist”
Why This Error Happens
Error 7939 occurs when SQL Server’s metadata in sys.databases does not contain an entry matching the database name supplied in the sp_detach_db command, typically due to a case-sensitivity mismatch, a typo, or trailing whitespace in the database name. This can also happen if the database was already dropped, detached by another session, or if you are connected to the wrong SQL Server instance (common in multi-instance installations on Windows Server 2025). Additionally, if the database exists in a different context (such as a contained availability group or a different collation catalog), SQL Server will not resolve the name correctly and will throw this error.
Step-by-Step Resolution Guide
Step 1: Verify the Exact Database Name and Instance
- Run
SELECT name, state_desc, database_id FROM sys.databases;to get the exact, case-accurate name of the database. - Confirm you are connected to the correct instance using
SELECT @@SERVERNAME;— on Windows Server 2025, check SQL Server Configuration Manager if multiple instances are installed. - Copy the exact name from the query output rather than retyping it manually to avoid hidden whitespace or encoding issues.
Step 2: Check for Prior Detachment or Drop Operations
- Review the SQL Server Error Log (via
EXEC sp_readerrorlog;or SSMS > Management > SQL Server Logs) for recentDROP DATABASEorsp_detach_dbentries. - Check Windows Event Viewer under Applications and Services Logs > Microsoft > Windows > SQLServer for related Event ID 7939 timestamps to correlate with other administrative activity.
- If another DBA or automated job (e.g., SQL Agent job, PowerShell script, or Always On failover) already detached the database, there’s no further action needed.
Step 3: Re-run the Detach Command with Correct Syntax
- Use the bracketed, exact name retrieved in Step 1:
EXEC sp_detach_db @dbname = N'[ExactDatabaseName]', @skipchecks = 'false';
- Ensure there are no leading/trailing spaces by wrapping the name in
QUOTENAME()if scripting dynamically. - If using SSMS GUI, right-click Databases, refresh the object explorer (F5), then retry the Detach option from the context menu.
Step 4: Refresh Metadata Cache and Object Explorer
- In SSMS, right-click the Databases node and select Refresh to force a metadata reload, since stale cached views can display databases that no longer exist server-side.
- If using a script-based connection, disconnect and reconnect the session to clear any cached
sys.databasessnapshot held by the query window.
Alternative or Verification Steps
- Verify success by re-querying
SELECT name FROM sys.databases WHERE name = 'YourDatabaseName';— it should return zero rows after a successful detach. - If the database still appears “stuck,” restart the SQL Server service via
services.mscon Windows Server 2025/Windows 11 (or useRestart-Service MSSQLSERVERin PowerShell) to clear any orphaned metadata cache, then repeat Step 1.