SQL Server Error 946 occurs when you attempt to attach or open a database file (.mdf) that was created in a newer version of SQL Server than the one currently running on your instance. To resolve this, you must either install a version of SQL Server that matches or exceeds the version of the database file, or migrate the data using a backup and restore process if you are moving to an older version (which requires a script-based approach). This error is a hard version check; SQL Server cannot "down-grade" a database file simply by attaching it.

What You'll Need

  • Administrative access to the Windows server or workstation hosting the SQL Server instance.
  • SQL Server Management Studio (SSMS) installed and configured.
  • The .mdf (Primary Data File) and .ldf (Log File) of the database you are trying to attach.
  • Knowledge of the version of SQL Server that originally created the database files.
  • A full backup of the files before attempting any modification to prevent data loss.

Step-by-Step

Depending on whether you want to upgrade your server to match the database or move the data to an older server, follow the appropriate path below. Note that menu labels in SSMS may differ slightly depending on the version installed.

Option 1: Upgrading the SQL Server Instance (Recommended)

This is the most direct solution. If the database was created in SQL Server 2022 and you are using SQL Server 2019, you must upgrade your instance to 2022.

  1. Identify the Required Version: Determine the exact version of the database file. If you do not know it, try attaching it to a newer instance of SQL Server (such as the latest Developer Edition) to see if it opens.
  2. Download the Installer: Visit the official Microsoft download center and obtain the installation media for the version of SQL Server that matches or is newer than the database file.
  3. Run the Installation Wizard: Launch the setup.exe file. Select "Installation" from the left-hand sidebar and choose "Upgrade from a previous version of SQL Server."
  4. Follow the Upgrade Path: Follow the prompts to select the instance you wish to upgrade. Ensure you have a full system backup before proceeding.
  5. Complete the Installation: Once the upgrade is complete, restart the SQL Server services.
  6. Attach the Database: Open SQL Server Management Studio (SSMS). Right-click on "Databases" in the Object Explorer, select "Attach...", click "Add", and select your .mdf file. The error 946 should now be gone.

Option 2: Moving Data to an Older Version (Scripting Method)

SQL Server does not support "downgrading" a database by attaching files. If you must move data to an older version, you cannot use the .mdf file; you must use a script.

  1. Open the Database in a Newer Version: Attach the .mdf file to a SQL Server instance that is compatible with the file version.
  2. Generate Scripts: Right-click the database in SSMS, go to "Tasks" > "Generate Scripts..."
  3. Configure Scripting Options: In the "Choose Objects" window, select "Script entire database and all database objects."
  4. Set Version Compatibility: In the "Set Scripting Options" step, click the "Advanced" button. Look for "Types of data to script" and change it to "Schema and data." Then, look for "Script for Server Version" and select the version of the destination (older) server.
  5. Save the Script: Save the output as a .sql file.
  6. Execute on Older Server: Connect to your older SQL Server instance, open the .sql script, and execute it to recreate the tables and populate the data.

Common Mistakes

  • Attempting to Edit the MDF File: Some users try to use a hex editor to change the version number inside the .mdf file. This will almost certainly corrupt the database and lead to permanent data loss. Never modify the binary files of a database.
  • Assuming SSMS Version Equals Server Version: A common point of confusion is the difference between SQL Server Management Studio (SSMS) and the SQL Server Engine. You can have the latest version of SSMS installed, but if the underlying SQL Server Engine (the service running in the background) is an older version, you will still get Error 946.
  • Ignoring the Log File: When attaching a database, if you only provide the .mdf and the .ldf is missing or from a different version, you may encounter different errors. Always keep the .mdf and .ldf files together.
  • Skipping Backups: Attempting an in-place upgrade of a SQL Server instance without a full VM snapshot or database backup is risky. If the upgrade fails, you may be left with an unusable instance.

Tips

  • Use Developer Edition: If you are troubleshooting a file and aren't sure which version it belongs to, install SQL Server Developer Edition. It is free for development use and includes all features of the Enterprise edition, making it the best tool for testing compatibility.
  • Check Internal Version Numbers: If you have access to the server where the database is currently running, run the command SELECT @@VERSION to see exactly which build and version you need to match.
  • Verify File Permissions: After upgrading or moving files to a new server, ensure the SQL Server Service account has "Full Control" permissions over the folder containing the .mdf and .ldf files, otherwise, you may see an "Access Denied" error immediately after solving the version error.
  • Check Compatibility Level: After successfully attaching a database to a newer version, right-click the database > Properties > Options. Check the "Compatibility level." You may want to update this to the latest version to take advantage of new query optimizer features.

Troubleshooting

If you have upgraded your server but still encounter issues attaching the database, try the following steps in order:

  1. Restart the SQL Server Service: Sometimes the engine does not recognize the upgraded environment until the service is fully restarted via the SQL Server Configuration Manager.
  2. Check for Multiple Instances: You might have upgraded "MSSQLSERVER" (the default instance) but are trying to attach the database to a named instance (e.g., "SQLEXPRESS") that is still running the old version. Verify which instance you are connected to in the SSMS login window.
  3. Verify File Integrity: If the error changes from 946 to something like "The log file is corrupted," try attaching the .mdf file alone. In the Attach Databases window, select the .ldf file in the bottom pane and click "Remove." SQL Server will attempt to rebuild a fresh log file (this only works if the database was shut down cleanly).
  4. Run SSMS as Administrator: If the attach process fails without a clear error or gives a permission error, close SSMS and relaunch it by right-clicking the icon and selecting "Run as Administrator."

Frequently Asked Questions

Can I just change a setting to allow older versions to open newer databases?

No. SQL Server's internal file structure changes between major versions. There is no toggle or setting to bypass this; the engine physically cannot read the newer page structures of a more recent version.

Is it possible to restore a backup (.bak) from a newer version to an older version?

No. Like the .mdf file, a backup created in a newer version of SQL Server cannot be restored to an older version. You must use the "Generate Scripts" method described in the Step-by-Step section.

Will upgrading my SQL Server instance delete my existing databases?

An in-place upgrade is designed to preserve your existing databases. However, it is a critical best practice to perform a full backup of all databases before starting the upgrade process to protect against unexpected installation failures.

How do I know which version of SQL Server I have?

Open a New Query window in SSMS and execute SELECT @@VERSION. This will return a string containing the product name (e.g., SQL Server 2019), the service pack level, and the specific build number.

Does SQL Server Express have the same version restrictions?

Yes. Whether you are using Express, Standard, or Enterprise editions, the versioning rules are the same. A database created in Enterprise 2022 cannot be opened in Express 2019.