dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Home/Error Library/sqlserver-cannot-open-database-requested-by-login
SQL Server
MSSQL 2017 - 2022Authentication & Security
5 min read
Cannot open database requested by the login. The login failed.

SQL Server "Cannot open database requested by the login": Valid Login, Inaccessible Database

Resolving Error 4060 & 18456 State 38/40: deleted default database, offline/restoring status, AUTO_CLOSE traps, and orphan user remediation.

Reviewed by:Senior Database Architect & SRE (dataiotech Labs)
Tested Versions: SQL Server 2017 / 2019 / 2022
First 60 Seconds Emergency Protocol:

The error string is a symptom, not the root cause. Do not restart the server blindly. Run the diagnostic queries below to pinpoint the actual failure mode.

▸Symptoms

plaintext
Cannot open database "uygulama_db" requested by the login. The login failed. Login failed for user 'uygulama'.

Authentication was successful, but the target database is inaccessible (State 38 / State 40).

▸Diagnostic & Resolution Steps

  1. 1Inspect Database State:
sql
SELECT name, state_desc, user_access_desc FROM sys.databases;

If RESTORING, execute RESTORE DATABASE [db] WITH RECOVERY;. If OFFLINE, run ALTER DATABASE [db] SET ONLINE;.

  1. 1Fix Default Database:

If the default database was dropped, point login to master:

sql
ALTER LOGIN [app] WITH DEFAULT_DATABASE = [master];
  1. 1Resolve Orphaned Users:

Following database restores across servers, SID mismatch causes login failures:

sql
USE [app_db]; ALTER USER [app] WITH LOGIN = [app];
  1. 1Disable AUTO_CLOSE:
sql
ALTER DATABASE app_db SET AUTO_CLOSE OFF;

Other Database Error Triage Playbooks

Other battle-tested diagnostic guides for high-severity database errors.

All Errors (20)