Restore SQL Server 2012 backup to a SQL Server 2008 database?
How to migrate a SQL Server database to a lower version
Problem
After recently upgrading a SQL Server instance to SQL Server 2012 a few days ago, you noticed that your application is not functioning properly. You decided to roll back the upgrade by downgrading the SQL Server database engine to SQL Server 2008 R2. After the downgrade of the database engine, you are unable to attach the databases or restore the backups of the databases, even though the database compatibility level is set to the downgraded version of SQL Server. You receive the following error message, when you attempt to restore the database:
Msg 1813, Level 16, State 2, Line 1
Could not open new database 'DatabaseName'. CREATE DATABASE is aborted.
Msg 948, Level 20, State 1, Line 1
The database 'DatabaseName' cannot be opened because it is version 655. This server supports version 611 and earlier. A downgrade path is not supported.
This error message is generated because SQL Server automatically upgrades the database, when you restore or attach the database from lower version to higher version. SQL Server does not allow you to restore or attach a database from a higher version of SQL Server to a lower version of SQL Server. In this tip, we will look at a one time procedure which we can follow to downgrade the database from a higher version (SQL Server 2012) of SQL Server to a lower version (SQL Server 2008 R2) of SQL Server.Could not open new database 'DatabaseName'. CREATE DATABASE is aborted.
Msg 948, Level 20, State 1, Line 1
The database 'DatabaseName' cannot be opened because it is version 655. This server supports version 611 and earlier. A downgrade path is not supported.
Option A : Script out database in compatibility mode using Generate script option :
Note : If you script out database with schema and data, depending on your data size, the script will be massive and wont be handled by SSMS, sqlcmd or osql (might be in GB as well).
Solution
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 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:- Generate Scripts wizard of SQL Server Management Studio
- SQL Server Integration Services
- Custom scripting and BCP
- Script the database schema in higher version of SQL Server by using the Generate Scripts wizard of SQL Server Management Studio interface.
- Connect to the lower version of SQL Server, and then run the SQL scripts that were generated in the previous step, to create database schema and copy data.
Note: For demonstration purpose, I'll be downgrading the OUTLANDER database hosted on my SQL Server 2012 instance (IITCUK\DEV01) to SQL Server 2008 R2 instance (IITCUK\SQLSERVER2008).
Step-by-Step Demo: Downgrading a SQL Server 2012 database to SQL Server 2008 R2
Step-1: Script the schema of the OUTLANDER database on the SQL Server 2012 instance (IITCUK\DEV01) using theGenerate Scripts wizard of the SQL Server Management Studio interface.In Object Explorer connect to IITCUK\DEV01, right-click OUTLANDER database, expand Tasks and choose"Generate Scripts...".
After reviewing your selections on Summary page, click Next to generate scripts.
In Object Explorer connect to IITCUK\SQLServer2008, then in SQL Server Management Studio, open the SQL Server script you saved in Step-1.
Option B:
First script out tables first with all Indexes, FK's, etc and create blank tables in the destination database - option with SCHEMA ONLY (No data).
Use BCP to insert data
- bcp out the data using below script. set SSMS in Text Mode and copy the output generated by below script in a bat file.
-- save below output in a bat file by executing below in SSMS in TEXT mode -- clean up: create a bat file with this command --> del D:\BCP\*.dat select '"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\bcp.exe" ' /* path to BCP.exe */ + QUOTENAME(DB_NAME())+ '.' /* Current Database */ + QUOTENAME(SCHEMA_NAME(SCHEMA_ID))+'.' + QUOTENAME(name) + ' out D:\BCP\' /* Path where BCP out files will be stored */ + REPLACE(SCHEMA_NAME(schema_id),' ','') + '_' + REPLACE(name,' ','') + '.dat -T -E -SServerName\Instance -n' /* ServerName, -E will take care of Identity, -n is for Native Format */ from sys.tables where is_ms_shipped = 0 and name <> 'sysdiagrams' /* sysdiagrams is classified my MS as UserTable and we dont want it */ /*and schema_name(schema_id) <> 'unwantedschema' */ /* Optional to exclude any schema */ order by schema_name(schema_id)
- Run the bat file that will generate the .dat files in the folder that you have specified.
- Run below script on the
--- Execute this on the destination server.database from SSMS. --- Make sure the change the @Destdbname and the bcp out path as per your environment. declare @Destdbname sysname set @Destdbname = 'destinationDB' /* Destination Database Name where you want to Bulk Insert in */ select 'BULK INSERT ' /*Remember Tables must be present on destination database */ + QUOTENAME(@Destdbname) + '.' + QUOTENAME(SCHEMA_NAME(SCHEMA_ID)) + '.' + QUOTENAME(name) + ' from ''D:\BCP\' /* Change here for bcp out path */ + REPLACE(SCHEMA_NAME(schema_id), ' ', '') + '_' + REPLACE(name, ' ', '') + '.dat'' with ( KEEPIDENTITY, DATAFILETYPE = ''native'', TABLOCK )' + char(10) + 'print ''Bulk insert for ' + REPLACE(SCHEMA_NAME(schema_id), ' ', '') + '_' + REPLACE(name, ' ', '') + ' is done... ''' + char(10) + 'go' from sys.tables where is_ms_shipped = 0 and name <> 'sysdiagrams' /* sysdiagrams is classified my MS as UserTable and we dont want it */ and schema_name(schema_id) <> 'unwantedschema' /* Optional to exclude any schema */ order by schema_name(schema_id)
- Run the output using SSMS to insert data back in the tables.
This is very fast bcp method as it uses Native mode.
Option C: