SQL Server error 912 occurs when a database fails to complete its script-level upgrade during startup, typically after a major version upgrade (e.g., from SQL Server 2016 to 2019). This prevents the database from coming online. The root cause is usually a mismatch between the database’s compatibility level, metadata, or internal schema and the new SQL Server instance’s expectations—often triggered by incomplete upgrades, interrupted migrations, or inconsistent system catalog states. The fix involves diagnosing the upgrade failure, verifying system integrity, and manually triggering or repairing the upgrade process. No special tools are required beyond SQL Server Management Studio (SSMS) and sysadmin privileges.

What You'll Need

  • SQL Server Management Studio (SSMS) installed and accessible
  • Sysadmin (sa) or equivalent high-privilege login
  • Access to the SQL Server error log (via SSMS or sys.fn_error_log)
  • Basic T-SQL knowledge to run diagnostic and repair commands
  • Backup of the affected database (critical before manual steps)

Step-by-Step

  1. Confirm the error and database state
    Open SSMS and connect to the SQL Server instance. In Object Explorer, expand Databases. If the database shows as (Recovering…), (Suspect), or (Emergency), right-click it and select Properties. Go to the Options page and check the Status. If the database is not online, run this query to verify its state and upgrade requirement:
    SELECT name, state_desc, is_auto_update_stats_async_on, compatibility_level FROM sys.databases WHERE name = 'YourDatabaseName';
    Look for state_desc = RESTORING or RECOVERING, and compatibility_level lower than the server’s version (e.g., 130 for SQL 2016 on a SQL 2019 instance).
  2. Check the SQL Server error log for upgrade details
    In SSMS, right-click the server node > Reports > Standard Reports > Activity Monitor. Alternatively, run:
    EXEC xp_readerrorlog 0, 1, 'script level upgrade';
    This returns the exact error message, including which internal script failed (e.g., migration of sys.sql_modules or sys.indexes). Note the line number and object involved—this guides recovery.
  3. Force the database offline and back online
    Run these commands in sequence to reset the recovery state:
    ALTER DATABASE [YourDatabaseName] SET OFFLINE WITH ROLLBACK IMMEDIATE;
    ALTER DATABASE [YourDatabaseName] SET ONLINE;
    Wait 10–15 seconds between commands. If the upgrade was interrupted, SQL Server may retry automatically on ONLINE.
  4. Manually trigger the upgrade script
    Use DBCC UPDATEUSAGE to refresh metadata, then run the upgrade script explicitly:
    DBCC UPDATEUSAGE ([YourDatabaseName]) WITH COUNT_ROWS;
    Then execute:
    ALTER DATABASE [YourDatabaseName] SET COMPATIBILITY_LEVEL = 150; -- Replace 150 with your target version: 140=2017, 150=2019, 160=2022
    Setting compatibility_level alone does not trigger full schema upgrades, but it initiates the next phase of validation.
  5. Run the internal upgrade script (if needed)
    SQL Server stores upgrade scripts in the \MSSQL\Binn\Templates folder (e.g., master.sql, model.sql). For user databases, use this T-SQL to force a full upgrade check:
    DBCC CHECKDB ([YourDatabaseName]) WITH DATA_PURITY, NO_INFOMSGS;
    This validates and repairs structural inconsistencies that block upgrades. If errors appear, add REPAIR_ALLOW_DATA_LOSS only after full backup (see troubleshooting).
  6. Verify the upgrade succeeded
    Re-run the diagnostic query from Step 1. Confirm state_desc = ONLINE and compatibility_level matches the target. Then run:
    SELECT SERVERPROPERTY('ProductVersion') AS ServerVersion, DATABASEPROPERTYEX('YourDatabaseName', 'Version') AS DatabaseVersion;
    DatabaseVersion should be ≥ 904 (SQL 2019 SP3) or ≥ 954 (SQL 2022). If not, the upgrade is incomplete.

Common Mistakes

  • Skipping the backup step before running REPAIR commands—this can cause permanent data loss.
  • Assuming compatibility_level change alone completes the upgrade; schema migrations require additional steps.
  • Running ALTER DATABASE commands while the database is in RESTORING state (must be OFFLINE first).
  • Misreading the error log—focusing on the first error instead of the root cause line (look for “Upgrade failed” or “Cannot open database”).
  • Ignoring pre-upgrade checks: databases with custom schemas or deprecated features (e.g., snapshot isolation) often fail during script-level upgrades.

Tips

  • Always upgrade databases in a test environment first using the same patch level as production.
  • Use sp_helpdb before and after changes to track compatibility_level and status changes.
  • Enable trace flag 3614 during startup to log detailed upgrade info: DBCC TRACEON (3614, -1); (remove after testing).
  • For Always On Availability Groups, ensure the secondary replica is synced before upgrading the primary database.
  • Run sp_updatestats after a successful upgrade to refresh statistics for the new query optimizer.

Troubleshooting

If the database remains offline or errors persist:

  • Check disk space: Ensure the tempdb and data file paths have ≥10% free space—upgrade scripts create temporary objects.
  • Verify permissions: The login must own the database (use ALTER AUTHORIZATION) or be db_owner.
  • Use emergency mode: If the database is suspect, run:
    ALTER DATABASE [YourDatabaseName] SET EMERGENCY;
    ALTER DATABASE [YourDatabaseName] SET SINGLE_USER;
    DBCC CHECKDB ([YourDatabaseName], REPAIR_ALLOW_DATA_LOSS);
    ALTER DATABASE [YourDatabaseName] SET MULTI_USER;
    ⚠️ This deletes corrupted data—only use after backup.
  • Restore from backup: If repair fails, restore the pre-upgrade backup and retry the upgrade using the SQL Server Installation Center’s “Upgrade Advisor” report to identify blocking issues first.
  • Check for corrupted system tables: Run DBCC CHECKCATALOG ([YourDatabaseName]). If it fails, the database may need rebuilding (see Microsoft’s “Rebuild System Databases” guide).

Frequently Asked Questions

Why does error 912 happen only after a major SQL Server upgrade?

Major version upgrades change internal metadata structures. If a database was created on an older version and not upgraded properly (e.g., detached/attached instead of using BACKUP/RESTORE), its schema lags behind, causing script-level failures during the first online attempt.

Can I upgrade a database without taking it offline?

No. Script-level upgrades require exclusive access. The database must be OFFLINE or in SINGLE_USER mode during the critical phases.

Does compatibility_level affect query performance after the upgrade?

Yes. Setting a lower compatibility_level on a newer SQL Server instance can prevent new optimizer features from applying. Always test workloads after upgrading to the latest compatibility level.

Is it safe to use REPAIR_ALLOW_DATA_LOSS?

Only as a last resort. It may delete corrupted rows or indexes. Always restore from backup if possible. Document the data loss and verify critical tables post-repair.

What if the error log shows “Cannot open database” instead of upgrade errors?

This indicates a different issue (e.g., file path changes or permissions). Check file existence with SELECT * FROM sys.database_files; and ensure the SQL Server service account has full control of the MDF/LDF folders.

How do I prevent this error during future upgrades?

Run the SQL Server Upgrade Advisor on your databases before migrating. Fix deprecated features, update statistics, and ensure all databases are ONLINE and healthy post-upgrade before proceeding to production.