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.
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
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
- 1Inspect Database State:
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;.
- 1Fix Default Database:
If the default database was dropped, point login to master:
ALTER LOGIN [app] WITH DEFAULT_DATABASE = [master];- 1Resolve Orphaned Users:
Following database restores across servers, SID mismatch causes login failures:
USE [app_db];
ALTER USER [app] WITH LOGIN = [app];- 1Disable AUTO_CLOSE:
ALTER DATABASE app_db SET AUTO_CLOSE OFF;Other Database Error Triage Playbooks
Other battle-tested diagnostic guides for high-severity database errors.
SQL Server Error 18456: Root Cause by State Code Matrix
In Error 18456, the State code reveals the actual root cause. Specific fixes for State 5, 8, 11, 18, 38, and 40, with ERRORLOG extraction scripts.
SQL Server Error 1205 Deadlock: Graph Extraction and Prevention
Extracting the deadlock graph from the system_health Extended Events ring buffer, understanding victim election, and architectural resolution.
SQL Server Pool Timeout: Connection Pool Exhaustion Fix
Difference between connection pool timeout and query command timeout, detecting connection leaks, and .NET DbContext architectural fixes.