Transfer backup data of a new version of MS SQL Server to an older version
Once, to reproduce a bug, I needed a backup of the production base.
To my surprise, I came across the following limitations:
- The database backup was made on the version of SQL Server 2016 and was not compatible with my SQL Server 2014 .
- On my working computer, Windows 7 was used as the OS , so I could not upgrade SQL Server to version 2016
- The supported product was part of a larger system with a heavily connected Legacy architecture and also turned to other products and bases, so deploying it to another station could take a very long time.
Given the above, I came to the conclusion that the time has come for
Data recovery from backup
I decided to use the Oracle VM VirtualBox with Windows 10 (you can take a test image for the Edge browser from here ). SQL Server 2016 was installed on the virtual machine and the application database was restored on it from the backup ( instructions ).
Configure Access to SQL Server in a Virtual Machine
Further, it was necessary to take some steps to make it possible to access SQL Server from the outside:
- For a firewall, add a rule to skip requests to port 1433 .
- It is desirable that access to the server does not go through windows authentication, but through SQL with a username and password (it’s easier to configure access). However, in this case, you must remember to enable SQL authentication in the properties of SQL Server.
- In the user settings on SQL Server, on the User Mapping tab, specify the user role db_securityadmin for the restored database .
Actually the data transfer itself consists of two stages:
- Data schema transfer (tables, views, stored procedures, etc.)
- Transfer data itself
Data schema migration
We perform the following operations:
- Select Tasks -> Generate Scripts for the portable database.
- We select the objects necessary for transferring or leave the default value (in this case, scripts will be created for all objects in the database).
- Specify settings for saving the script. It is most convenient to save the script into a single file encoded in Unicode. Then, in the event of a failure, it is not necessary to repeat all the steps again.
After saving the script, it can be executed on the original SQL Server (old version) to create the required database.
Attention: after running the script, you need to check the compliance of the database settings from the backup and the database created by the script. In my case, there was no setting for COLLATE in the script, which led to a crash when transferring data and
Before transferring data, you must disable the check of all restrictions based on:
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'
We transfer data using the Tasks -> Import Data data import wizard on SQL Server, where the database created by the script is located:
- Specify the source connection settings (SQL Server 2016 on the virtual machine). I used Data Source SQL Server Native Client and the aforementioned SQL authentication.
- Specify the settings for connecting to the destination (SQL Server 2014 on the host machine).
- Next, configure the mapping. You must select all non-read-only objects (for example, you do not need to select views). As additional options, select "Allow insertion into identity columns" , if used.
Attention: if, when you try to select several tables and set the “Allow insertion into identity-columns” property on them, the property has already been set for at least one of the selected tables, the dialog will indicate that the property is already set for all selected tables. This fact can be confusing and lead to transfer errors.
- We start the transfer.
- Restore constraint check:
EXEC sp_msforeachtable 'ALTER TABLE ? CHECK CONSTRAINT all'
If any errors occur, check the settings, delete the database created with the errors, re-create it from the script, make corrections and repeat the data transfer.
This task is quite rare and arises only due to the above limitations. The most common solution is to upgrade SQL Server or connect to a remote server if the application architecture allows it. However, no one is safe from the legacy code and
List of sources used
- How do I deal with FK constraints when importing data using DTS Import / Export Wizard?
- The column “Column 2” cannot be processed because more than one code page (65001 and 1252) are specified for it.
- How can I connect to SQLServer running on VirtualBox from my host Macbook.
- SQL SERVER - Enable Identity Insert - Import Expert Wizard
- Troubleshooting Microsoft SQL Server Error 18456, Login failed for user