How to Automate the Process of SQL Server Database Schema Synchronization

How to Automate the Process of SQL Server Database Schema Synchronization
2.5 (50%) 2 vote[s]

The goal

Keeping two databases in sync is a task we often encounter when working on SQL database development and maintenance. One particular case is this – the versions of a database present on testing and development environments need to be consistently synchronized to make sure that tests are running on the most recent version. This should be done by detecting schema changes in the development version of a database and then automatically synchronizing them with the version from the testing environment on a scheduled basis.

How to achieve it

We’ll look into how automated synchronization of two SQL databases can be achieved by combining a schema comparison tool with PowerShell scripts and scheduling.

In the example we’re going to see, the databases will be synchronized every 15 minutes. As we’re using a schema comparison tool, we can be sure that the database will only be updated if there are some actual changes between the development and testing versions of the database – this prevents the execution of unnecessary resource-heavy operations.

We’ll use Devart Schema Compare as the utility of choice to diff and synchronize SQL Server databases. It’s a tool that allows you to compare and sync live SQL Server databases, snapshots, and backups. Let’s look at how we can implement our project below.


First of all, we’ll need to set some things up:

  1. Run Schema Compare
  2. Click the New Schema Comparison button on the toolbar or, alternatively, in the top right part of the start page:New Schema Comparison
  3. In the Source and Target tab of the New Schema Comparison window, choose the desired source and target databases:Source and Target Pane
  4. In the Options, Schema Mapping, Table Mapping and Object Filter tabs, you can set up the necessary comparison and synchronization options:
  5. When everything is set up, you can press the Compare button in the bottom right corner to start the comparison process.
  6. When the comparison is finished, you can see all objects that have been compared and their respective difference status in the top part of the window, while the detailed script for each of those objects will be located at the bottom:Comparison Results
  7. Check all objects to include them into the synchronization process and click the Save button or press File > Save:Save Comparison ProjectThis will save the project with all the information we need to start the synchronization process.
  8. Once the project is saved, click the ‘Synchronize objects‘ button:Synchronize Schemas
  9. Schema Synchronization Wizard will be opened. First, select ‘Execute the script directly against the target database‘ in the Output tab:Synchronization Output Options
  10. You can choose the necessary settings in the Options tab:Synchronization Options
  11. You can check all objects that are going to be synchronized in the Summary tab:Synchronization Summary
  12. Click Synchronize in the bottom-right corner to test the synchronization process.
  13. You will see the results displayed in the bottom pane of the window:Synchronization Results

Automating the process

As schema synchronization was successful and now that we have a project file with all the necessary info, let’s automate the synchronization process by using a PowerShell script.

In the following example, it is assumed that you use Integrated security but the stored and encrypted database can still be easily accessed from the project file, along with repository credentials.

We’ll look at some parts of the script creation process that may be particularly interesting, but feel free to skip this section to download and test the finished script right away.

Creating a script

First, we’ll need to create a function that checks if the Outputs folder exists. This folder’s purpose is to store data-stamped commit summaries. The function should look like this:

Next, we’ll define the root folder and the location of schema output summaries:

Now, we’ll need to define Schema Compare’s location, the date stamp variable and parameters of the application:

With this in place, we can set the path to the output log file:

Next, we’ll call Schema Compare and let it execute its synchronization parameters:

The final part of the script will serve to define all possible outcomes:

  1. Schema changes are detected, return code 0 – Success
  2. No schema changes detected, return code 100– No schema changes detected
  3. An error has been encountered and the output summary will be opened.


With the PowerShell script ready and the process automated, we can schedule this in a few different ways, e.g. via the Windows Scheduler.

Viewing results

Now that the scheduled job is up and running, you can view schema output summaries anytime you need. In the example we just seen, $outsLoc variable defined the location of schema output summaries. As a result, such summaries will be saved to $rootFolder\$outsLoc – in this particular case, SchemaSync\Outputs:

Outputs FolderIf any error is encountered, e.g. a project file’s extension is entered incorrectly, the corresponding error message will be displayed in the output summary.

The list of return error codes will help us to better understand the specific error we encountered.


If any questions or issues arise during the process of setting this up, feel free to contact us anytime at [email protected]m