4

I can't attach my database. When I try to attach a database in SQL Server Management Studio, I get this error:

The database 'C:\FILES\ACCOUNTING.MDF' cannot be opened because it is version 904. This server supports version 852 and earlier. A downgrade path is not supported. Could not open new database 'C:\FILES\ACCOUNTING.MDF'. CREATE DATABASE is aborted. (.Net SqlClient Data Provider)

and I have tried these commands:

cd "C:\Program Files\Microsoft SQL Server\130\LocalDB\Binn"

SqlLocalDB.exe delete "MSSQLLocalDB"

SqlLocalDB.exe create "MSSQLLocalDB"

but it still has an error

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mohammad Gh
  • 320
  • 2
  • 4
  • 11
  • IN SSMS right click database (Tasks > Generate Scripts) - you should get in Advanced and set **Schema and Data** and etc – Amirhossein Mar 07 '20 at 07:29
  • Related: [Trying to attach a database fails because it is version 782](https://stackoverflow.com/q/25500912/1364007), [The database cannot be opened because it is version 782. This server supports version 706 and earlier. A downgrade path is not supported](https://stackoverflow.com/q/26346647/1364007), – Wai Ha Lee Jun 24 '21 at 17:08

2 Answers2

7

You CANNOT do this - you cannot attach/detach or backup/restore a database from a newer version of SQL Server (v904 = SQL Server 2019) down to an older version (v852 which is SQL Server 2016) - the internal file structures are just too different to support backwards compatibility.

You can either get around this problem by

  • using the same version of SQL Server on all your machines - then you can easily backup/restore databases between instances

  • otherwise you can create the database scripts for both structure (tables, view, stored procedures etc.) and for contents (the actual data contained in the tables) either in SQL Server Management Studio (Tasks > Generate Scripts) or using a third-party tool

  • or you can use a third-party tool like Red-Gate's SQL Compare and SQL Data Compare to do "diffing" between your source and target, generate update scripts from those differences, and then execute those scripts on the target platform; this works across different SQL Server versions.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
4

The error message in the problem statement occurs because the SQL Server database files (*.mdf, *.ndf and *.ldf) and backups are not backward compatible. Backward compatibility is why we cannot restore or attach a database created from a higher version of SQL Server to a lower version of SQL Server. However, there are a few options that can help us to downgrade the database from a higher version of SQL Server to a lower version SQL Server. These options include:

Use the Generate Scripts Wizard in SQL Server Management Studio Use SQL Server Integration Services Create Custom Scripting and BCP In this tip we will use the Generate Scripts Wizard in SQL Server Management Studio.

Here are the basic steps we need to follow:

Script the database schema and data from the higher version of SQL Server by using the Generate Scripts Wizard in SSMS. Connect to the lower version of SQL Server, and run the SQL scripts that were generated in the previous step, to create the database schema and data. In the next section, I will demonstrate the steps for downgrading a SQL Server 2012 database to SQL Server 2008 R2 database.

Steps to Downgrade a SQL Server Database Using SSMS Generate Scripts Wizard

Step 1 Script the schema of the OUTLANDER database on the SQL Server 2012 instance (IITCUK\DEV01) using the Generate Scripts wizard in SSMS.

In Object Explorer connect to IITCUK\DEV01, right-click on the OUTLANDER database, expand Tasks and choose "Generate Scripts...".

enter image description here

This launches Generate and Publish Scripts wizard. Click Next, to skip the Introduction screen and proceed to the Choose Objects page.

enter image description here

On the Choose Objects page, choose option "Script entire database and all database objects", and then click Next to proceed to "Set Scripting Options" page.

enter image description here

n the Advanced Scripting Options dialog box,

set Script for Server Version to SQL Server 2008 R2 (or whatever version you want) under the Table/View Options, set Script Triggers, Script Indexes and Script Primary Keys to True and set Types of data to script to Schema and Data - this last option is key because this is what generates the data per table.

enter image description here

Once done, click OK, to close the Advanced Scripting Options dialog box and return to Set Scripting Options page. In Set Scripting Options page, click Next to continue to Summary page.

After reviewing your selections on Summary page, click Next to generate scripts.

enter image description here

Once scripts are generated successfully, choose the Finish button to close the Generate and Publish Scripts wizard.

Step 2 Connect to the SQL Server 2008 R2 instance (IITCUK\SQLSERVER2008), and then run the SQL scripts that were generated in Step 1, to create the OUTLANDER database schema and data.

In Object Explorer connect to IITCUK\SQLServer2008, then in SQL Server Management Studio, open the SQL Server script you saved in Step 1

Modify the script, to specify the correct location for the OUTLANDER database data and log files. Once done, run the script to create the OUTLANDER database on IITCUK\SQLServer2008 instance.

enter image description here

Upon successful execution, refresh the Database folder in Object Explorer. As you can see in the following image, the OUTLANDER database has been successfully downgraded.

Notes There are a few things to be aware of when using this approach.

This solution creates one large SQL file that has the scripts to create the database objects and also INSERT statements for the data in the tables. For a large databases, the SQL file can get very large if you script out both the schema and the data and could be hard to load into an editor. Also, you may get a memory related error message from the editor if the file is too big. For large databases, around 1GB or more, if this approach does not work, then you should look at using SSIS to migrate the database or create custom scripts to script out the objects and BCP out the data for each of the tables. You can use this Generate Scripts wizard to just generate the schema without the data and use SSIS or BCP to export and import the data. This approach works for SQL Server 2017 to SQL Server 2005. Some of the scripting options might be a bit different in newer versions, but the process is still the same. Before just executing the script, you should review the script to make sure everything looks correct such as the path of the database files, database options, etc. Also if you are using new functionality that does not exist in the lower version, SQL Server won't be able to create the objects and you will need to review the scripts that were generated and update the code accordingly. For a very simple database this approach should work pretty easliy, but you might need to spend some time making some modifications to the script for a more complex database. Below is a list of all of the scripting options. If you click on an item, the bottom part of the screen gives you a short definition of the option.

enter image description here

Next Steps To avoid this issue, always make sure that you perform a full backup of the database before you upgrade the SQL Server and database to a higher version of SQL Server. In addition, be sure to thoroughly test the application prior to releasing the application to the users. Consider this downgrade option as your last option to rollback from an upgrade because the time and storage needed can be very large. With a very large database be sure you have sufficient storage to support the data needs. Be sure to verify row and object counts as well as test your application before releasing to production.

Additional Resources:

Why Can't I Restore a Database to an Older Version of SQL Server?

SQL Server Database Engine Backward Compatibility

SQL Server Upgrade Tips

Community
  • 1
  • 1
Amirhossein
  • 1,148
  • 3
  • 15
  • 34
  • Thanks a lot. finally, I deleted SQL Server 2019. now I working with SQL Server 2016 and Problem resolved – Mohammad Gh Mar 07 '20 at 17:02
  • @MohammadGh read this link for advantage of 2019 - https://www.instagram.com/p/B9XITVFFcGV/?utm_source=ig_web_copy_link – Amirhossein Mar 08 '20 at 08:18