Troubleshooting Guide: Error 12104 — “ALTER DATABASE CURRENT failed because ‘%.*ls’ is a system database”
Why This Error Happens
This error occurs because the ALTER DATABASE CURRENT syntax is designed to operate on the database context currently active in the session, but SQL Server explicitly blocks this shortcut when the active context resolves to a protected system database (master, model, msdb, or tempdb). This is a deliberate safety restriction in SQL Server 2025 to prevent scripts or automated jobs from accidentally altering critical system databases without explicitly naming them. The root cause is almost always a script, migration tool, or deployment pipeline that executes ALTER DATABASE CURRENT while the session’s default or active database happens to be a system database instead of the intended user database.
Step-by-Step Resolution Guide
Step 1: Identify the Current Session Context
- Run
SELECT DB_NAME();in the same session/script to confirm which database is actually active. - Check if the login’s default database (via
sys.server_principals) is set tomasterinstead of a user database. - Review connection strings in your application or ETL tool for a missing or incorrect
Initial Catalog/Databaseparameter.
Step 2: Correct the T-SQL Script to Use an Explicit Database Name
- Replace the problematic line, for example change:
ALTER DATABASE CURRENT SET RECOVERY SIMPLE;
to:
ALTER DATABASE [YourUserDatabaseName] SET RECOVERY SIMPLE;
- Ensure deployment scripts (DACPAC, SSDT, Flyway, Liquibase) explicitly reference the target database name rather than relying on
CURRENT.
Step 3: Fix the Login’s Default Database Setting
- Open SSMS and navigate to Security > Logins > [YourLogin] > Properties > General.
- Update “Default database” from
masterto the correct user database. - Alternatively, run:
ALTER LOGIN [YourLoginName] WITH DEFAULT_DATABASE = [YourUserDatabaseName];
Step 4: Add an Explicit USE Statement Before Execution
- Insert
USE [YourUserDatabaseName];at the top of the script, before anyALTER DATABASE CURRENTcommand. - Confirm that automated jobs (SQL Agent, Azure Data Factory, PowerShell
Invoke-Sqlcmd) pass the correct-dor-Databaseparameter pointing to the user database, notmaster.
Alternative or Verification Steps
- Re-run the script and confirm
SELECT DB_NAME();returns the expected user database name before theALTER DATABASEstatement executes. - If the error persists in automated pipelines, audit the connection string/credential file for a hardcoded
Database=masterentry and update it to the correct target database.