Troubleshooting Guide: SQL Server Error 2702 — “Database ‘%.*ls’ does not exist.”
Why This Error Happens
Error 2702 occurs when SQL Server 2025 cannot locate the database specified in a query, connection string, or USE statement within its system catalog. This typically happens because the database name is misspelled, the database was detached or dropped, or the user’s connection context defaults to a database that no longer exists on that instance. It can also surface after a failed restore operation or when connecting to the wrong SQL Server instance entirely (e.g., a named instance vs. the default instance).
Step-by-Step Resolution Guide
Step 1: Verify the Database Exists on the Instance
- Open SQL Server Management Studio (SSMS) or Azure Data Studio and connect to the target instance.
- Run the following query to list all available databases:
SELECT name, state_desc FROM sys.databases;
- Confirm the exact spelling, including case sensitivity if the instance uses a case-sensitive collation.
- Check that you are connected to the correct instance (e.g.,
SERVERNAME\INSTANCENAME) and not a different local or default instance.
Step 2: Check for Detached, Offline, or Corrupted Databases
- If the database is listed but shows a
state_descofOFFLINE,RESTORING, orSUSPECT, it exists but is inaccessible in its current state. - Navigate to
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\(adjust version folder for SQL Server 2025) and confirm the.mdfand.ldffiles are present. - Bring an offline database back online with:
ALTER DATABASE [YourDatabaseName] SET ONLINE;
- If files are missing, attempt to reattach using:
CREATE DATABASE [YourDatabaseName]
ON (FILENAME = 'C:\Path\To\YourDatabaseName.mdf')
FOR ATTACH;
Step 3: Correct the Connection String or Default Database Setting
- Open the application’s configuration file (e.g.,
web.config,app.config, or a.envfile) and verify theInitial CatalogorDatabaseparameter matches the exact database name. - In SSMS, right-click the login under Security > Logins, select Properties, and check the Default database dropdown to ensure it points to a valid, existing database.
- Restart the application or service to force it to re-read the updated connection string.
Step 4: Restore the Database if It Was Deleted
- If the database was accidentally dropped, locate the most recent
.bakfile (commonly stored inC:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Backup\). - Run a restore operation:
RESTORE DATABASE [YourDatabaseName]
FROM DISK = 'C:\Path\To\Backup.bak'
WITH RECOVERY;
- Verify no other process or backup job is actively dropping/recreating the database in a maintenance window.
Alternative or Verification Steps
- After applying a fix, run
SELECT DB_ID('YourDatabaseName');— a non-NULL result confirms the database is now recognized by the instance. - If the issue persists, use SQL Server Configuration Manager on Windows Server 2025 to confirm the service is pointing to the correct data directory and that no duplicate instance is intercepting the connection request.