How to fix Error 12122 on Windows Server 2025 / Windows 11

Troubleshooting Guide: Error 12122 — “%ls cannot be enabled or disabled on system databases”

Why This Error Happens

This error occurs when an administrator or an automated maintenance script attempts to enable or disable a database-level feature (such as CHANGE_TRACKING, CDC, or a similar toggleable option) directly against a protected system database like master, model, msdb, or tempdb. SQL Server 2025 intentionally blocks this operation because system databases have reserved configurations that are required for engine stability, and altering them could corrupt internal metadata or break instance-level functionality. In most cases, this is triggered by a script written with a hardcoded database name variable that accidentally points to a system database instead of the intended user database.

Step-by-Step Resolution Guide

Step 1: Identify the Target Database in the Failing Script

  • Open the T-SQL script or SQL Agent job step that threw Error 12122.
  • Check the USE [DatabaseName] statement or the ALTER DATABASE command for the exact database name being referenced.
  • Confirm whether the name resolves to master, model, msdb, or tempdb rather than your intended application database.

Step 2: Correct the Database Context

  • Update the script to explicitly target the correct user database, for example: USE [YourAppDatabase];
  • If the script uses a dynamic variable (e.g., @DBName), add a validation check to exclude system databases:
IF DB_NAME() IN ('master','model','msdb','tempdb')
BEGIN
    RAISERROR('Operation not permitted on system databases.', 16, 1);
    RETURN;
END
  • Save the corrected script and re-run it in a test environment first.

Step 3: Validate Permissions and Automation Jobs

  • Open SQL Server Management Studio (SSMS) and navigate to SQL Server Agent > Jobs.
  • Review any maintenance plans or scheduled jobs that loop through “all databases” using sys.databases, since this often causes system databases to be included unintentionally.
  • Add a WHERE database_id > 4 filter to exclude system databases from the loop, since IDs 1–4 are reserved for master, tempdb, model, and msdb.

Step 4: Restart the Affected Service (If Configuration Was Cached)

  • Open Services.msc on Windows Server 2025 / Windows 11.
  • Restart the SQL Server (MSSQLSERVER) service (or the named instance service) to clear any cached session state.
  • Re-attempt the original enable/disable operation against the correct user database.

Alternative or Verification Steps

  • Run SELECT name, database_id FROM sys.databases; in SSMS to visually confirm the target database is not one of the four system databases before re-running the feature toggle command.
  • If the error persists on a legitimate user database, verify the database isn’t in SINGLE_USER or RESTRICTED_USER mode, and confirm the login has ALTER permission scoped correctly using sp_helprotect or the Security > Permissions tab in SSMS.