ChatGPT解决这个技术问题 Extra ChatGPT

SQL Server database backup restore on lower version

How to restore a higher version SQL Server database backup file onto a lower version SQL Server?

Using SQL Server 2008 R2 (10.50.1600), I made a backup file and now I want to restore it on my live server's SQL Server 2008 (10.00.1600).

When I tried to restore the backup onto SQL Server 2008 it gives an error i.e. Restore Failed because:

The database was backed up on a server running version 10.50.1600. That version is incompatible with this server, which is running version 10.00.1600.

How do I restore the backup file on this server?

You cannot do this - it's just not possible. SQL Server doesn't allow *downgrading" a database from a higher version to a lower one.
Microsoft knows everything about SQL server (since they created it) and yet moving data between 2 versions is impossible. I simply don't understand why some sort of compatibility mode not available in export. How hard it can be?
@dvdmn as hard as any Top Gear episode where they say that - different types, different statements, different block layouts. Moving data between databases is very easy, no matter the versions involved. Downgrading though is not.
@PanagiotisKanavos I disagree, in mysql you can export db as sql commands and you can restore it on any version of mysql (down or up). Yes you can export sql db as script too but restoring it not easy if you have some stored function/procedures that depends on each other. It is basically not useful/solution.
@dvdmn exporting a database's scripts is completely different from backup. I realize some products don't offer backup/restore functionality or worse, muddle the two concepts. This often happens due to historical reasons: lacking backup in the past, some products called exporting/scripting by that name. Now that they do, they have to keep solving term conflicts, by talking about physical and logical backups, then get mixed up when they talk about scripting

v
veljasije

You can use functionality called Export Data-Tier Application which generates .bacpac file consisting database schema and data.

On destination server, you can use Import Data-Tier Application option which creates and populates new database from pre-created .bacpac file

If you want just to transfer database schema, you can use Extract Data-Tier Application for creating file and Deploy Data-Tier Application for deploying created database schema.

I've tried this process on different versions of SQL Server from SQL 2014 to SQL 2012 and from SQL 2014 to SQL 2008R2 and worked well.


This is an answer that deserves more attention. It actually does Just Work for most cases. If you have SQL users on there that are orphaned, though, the Export operations will fail. You can always remove/add those bits manually to otherwise transcend SQL versions.
I just tried this, to create a DB created on MSSQLEX2014 (V12) on my MSSQLEX2012 (V11). I just get a "Microsoft.Data.Tools.Schema.Sql.Sql120DatabaseSchemaProvider is not valid" error with no way to change it.
Importing from SQL Server 2014 to 2012 using a .bakpac DOES work, but it requires the correct version of SSMS. For example, I use SSMS 2016 CTP3 and it worked flawlessly. It did NOT work for me using SSMS 2012. I have not tested SSMS 2014.
Anyone knows if one can script this procedure in T-SQL? UPDATE (answering myself after a bit of googling): you can automate this via sqlpackage.exe command line. Google for more.
This is the only solution that worked for me for downgrading quite large/complicated SQL2017 to SQL2016. Performance also very good.
R
Remus Rusanu

No, is not possible to downgrade a database. 10.50.1600 is the SQL Server 2008 R2 version. There is absolutely no way you can restore or attach this database to the SQL Server 2008 instance you are trying to restore on (10.00.1600 is SQL Server 2008). Your only options are:

upgrade this instance to SQL Server 2008 R2 or

restore the backup you have on a SQL Server 2008 R2 instance, export all the data and import it on a SQL Server 2008 database.


@Gaurav Don't script out the data - instead, script out the entire database (tables, procs, triggers, constraints, etc) without data, create the new database, then right click the source database, go to "Tools", then "Export Data" to transfer the contents of the tables to your database.
I am very suspicious of a pat answer "there is no possible way" - that just means you haven't yet found the way. There may be many reasons why you want to do this, so this is not a helpful answer.
I disagree, @Jay, sometimes "you can't" is the perfect answer, though it's more useful to offer workarounds where available.
@Zhang if you have questions, ask them as questions, not as comment.
the veljasije's answer has a simple workaround. Don't understand why a "no is not possible" is marked as answer, when there is a simple solution to achieve it without any upgrade.
S
Smit Patel

You can not restore database (or attach) created in the upper version into lower version. The only way is to create a script for all objects and use the script to generate database.

https://i.stack.imgur.com/RCX6D.png

select "Schema and Data" - if you want to Take both the things in to the Backup script file select Schema Only - if only schema is needed.

https://i.stack.imgur.com/8cAY5.png

Yes, now you have done with the Create Script with Schema and Data of the Database.


It's 2020 and that's the best answer now!
@d.popov only if the database is a couple of gigabytes, otherwise the file will be too large to be read by SSMS.
t
theMayer

Will not necessarily work

Backup / Restore - will not work when the target is an earlier MS SQL version.

Copy Database - will not work when the target is SQL Server Express: "The destination server cannot be a SQL Server 2005 or later Express instance."

Data import - Will not copy the schema.

Will work

Script generation - Tasks -> Generate Scripts. Make sure you set the desired target SQL Server version on the Set Scripting Options -> Advanced page. You can also choose there whether to copy schema, data, or both. Note that in the generated script, you may need to change the DATA folder for the mdf/ldf files if moving from non-express to express or vice versa.

Microsoft SQL Server Database Publishing Services - comes with SQL Server 2005 and above, I think. Download the latest version from here. Prerequisites: sqlncli.msi/sqlncli_x64.msi/sqlncli_ia64.msi, SQLServer2005_XMO.msi/SQLServer2005_XMO_x64.msi/SQLServer2005_XMO_ia64.msi (download here).


Script generation is unreliable for substantial databases, avoid if at all possible.
Both links at the bottom are 404.
According to docs.microsoft.com/en-us/sql/relational-databases/databases/… the Copy Database option will not work because "A database cannot be moved or copied to an earlier version of SQL Server"
K
Ken Williams

Here are my 2 cents on different options for completing this:

Third party tools: Probably the easiest way to get the job done is to create an empty database on lower version and then use third party tools to read the backup and synchronize new newly created database with the backup.

Red gate is one of the most popular but there are many others like ApexSQL Diff , ApexSQL Data Diff, Adept SQL, Idera …. All of these are premium tools but you can get the job done in trial mode ;)

Generating scripts: as others already mentioned you can always script structure and data using SSMS but you need to take into consideration the order of execution. By default object scripts are not ordered correctly and you’ll have to take care of the dependencies. This may be an issue if database is big and has a lot of objects.

Import and export wizard: This is not an ideal solution as it will not restore all objects but only data tables but you can consider it for quick and dirty fixes when it’s needed.


Red Gate Sql Clone does not support cloning between different version of SQL server, reporting exactly the same error which I get when trying to do this manually.
T
Taryn

Another way to do this is to use "Copy Database" feature:

Find by right clicking the source database > "Tasks" > "Copy Database".

You can copy the database to a lower version of SQL Server Instance. This worked for me from a SQL Server 2008 R2 (SP1) - 10.50.2789.0 to Microsoft SQL Server 2008 (SP2) - 10.0.3798.0


Note that this will not work if the target is SQL server express "The destination server cannot be a SQL Server 2005 or later Express instance." social.msdn.microsoft.com/Forums/en-US/sqlgetstarted/thread/…
It also is not possible to copy a database from SQL Server 2012 to SQL Server 2008 this way.
Copy database is a Detach/Attach operation
Thanks for this hint! That helped me very much. I copied the data from SQL 2008 R2 to SQL 2008.
I don't see this option in SSMS 18.4. YMMV.
I
Ishtiyaq Khan

You can try this.

Create a Database onto SQL Server 2008. Using Import Data feature import data from SQL Server R2 (or any higher version). use "RedGate SQLCompare" to synchronize script.


K
Kate Orlova

Go to Task->Generate Scripts...

In Advanced in "Types of data for script" select "Schema and data" and try to run this script in your lower version.


This is very good solution but works very poor with larger databases.
m
motogeek

It's not pretty, but this is how I did it granted you have this option installed on your SQL 2008 R2 install..

1) Right click database in SQL Server 2008 R2 "Tasks".. "Generate scripts" in the wizard, select the entire database and objects in first step. On the "Set Scripting Options" step you should see a button "Advanced" , select this and make sure you select "Script for Server Version" = SQL Server 2008" not R2 version. This is a crucial step, because "import data" by itself does not bring along all the primary keys, constriants and any other objects like stored procedures."

2) Run the SQL script generated on the new install or database instance SQL Express or SQL Server 2008 using the query window or open saved .sql script and execute and you should see the new database.

3) Now right click on the new database and select "Tasks".. "Import Data.." choose source as the R2 database and the destination as the new database. "Copy data from one or more tables or views", select the top checkbox to select all tables and then next step, run the package and you should have everything on a older version. This should work for going back to a 2005 version as well. Hope this helps someone out.


hi @motogeek, i have the same issue, but i have the R2 on production server and 2008 on local, in this case i cannot use the import and export data. what would be the ideal solution for this?
a
abpatil

you can use BCP in and out for small tables.

BCP OUT command:-

BCP "SELECT *  FROM [Dinesh].[dbo].[Invoices]" QUERYOUT C:\av\Invoices1.txt -S MC0XENTC -T -c -r c:\error.csv

BCP IN command:- Create table structure for Invoicescopy1.

BCP [Dinesh].[dbo].[Invoicescopy1] IN C:\av\Invoices.txt -S MC0XENTC -T -c

t
tony.wiredin

I appreciate this is an old post, but it may be useful for people to know that the Azure Migration Wizard (available on Codeplex - can't link to is as Codeplex is at the moment I'm typing this) will do this easily.


That tool has been replaced with the SQL Server Data Migration Assistant. See: stackoverflow.com/questions/46517060/…
t
tonderaimuchada

You'd have to use the Import/Export wizards in SSMS to migrate everything

There is no "downgrade" possible using backup/restore or detach/attach. Therefore what you have to do is:

Backup the database from the server running the new SSMS/SQL version. Import data from the generated .bak file, by expanding the "Tasks" menu(after right-clicking the target database) and selecting the "Import Data" option.


S
Sajeeb Chandan Saha

You can generate script from Task menu

For detailed reference

How to migrate a SQL Server database to a lower version