How to automatically synchronize data in two SQL Server databases on a schedule

June 3rd, 2019

Data in SQL databases often needs to be synchronized in real time – this can be achieved by checking one database for updates and then applying them to another database. In this case, the process of change detection and synchronization should be ran automatically on a schedule and with no need for outside intervention.

How we can achieve this goal

Data Compare is an external tool which allows you to compare data in SQL databases, backups and script folders. With dbForge Data Compare for SQL Server, you can schedule almost real-time database synchronization.

You can set up the process by following these steps:

Run Data Compare

• In the New Data Comparison window, choose the source and target databases in the corresponding tab:

New-Data-Comparison

• You can set up various comparison settings in the Options tab, if this is needed.

• In the Mapping tab, you will be able to select which objects should be compared. Also, you can specify the key columns and the list of columns for comparison, if this is necessary:

Mapping Settings

• When everything’s ready, press the Compare button in the bottom-right corner to start the comparison process

• When the comparison is done, you can see the results in detail:

Comparison Results

• Select all necessary objects by using the corresponding check marks and click Save:

Save Comparison Project

The saved project (dcomp) file will contain all objects and options needed for scheduling data synchronization.

• When the project (dcomp) file is saved, press the Synchronize button which will open the Synchronization wizard:

Synchronize Data

• Choose ‘Execute the script directly against the target database’ so that the databases will be synchronized after you set up all necessary options:

Synchronization Output Options

• Now, press Synchronize in the bottom right corner

• When the synchronization process is over, you can view synchronization results in the bottom pane of the window.

Automating the process

As we have already successfully tested the synchronization process in Data Compare and saved the project (dcomp) file, let’s automate the process with a PowerShell script.

Setting things up

First, we’ll need to create a function that will check if the Outputs folder exists – it will be used to store date-stamped output summaries. We want to make sure that an easy-to-find application log of every synchronization is saved in case we will need to perform troubleshooting in the future:

Next, let’s define the root folder and the location for data-stamped output summaries:

Variable and switches

In this section, we define the application’s location along with the data stamp variable. Also, we define the variable containing the application’s parameters, such as:

  • the path to the saved project (dcomp) file;
  • the /sync switch for direct synchronization of the destination database;
  • the /rece switch which returns the ‘102 – No differences detected’ message when data sources are equal
  • a date-stamped output summary.

The following script allows us to achieve this:

Execution

The next part of the PowerShell script will call Data Compare from its location with the parameters we stated in the previous step. Then, the return code variable is defined:

The script’s final part serves to create proper responses for the three possible outcomes:

• An error occurred and the output summary will be opened.

• There are differences, e.g. return code 0 – Success

• There are no differences, e.g. return code 100 – No differences detected

Now that the job has been automated, it can be scheduled in in any way you prefer – for example, with the help of Windows Scheduler.

Reviewing results

Once everything is up and running, an output summary can be reviewed anytime. In this example, the location of output files is defined by the $outsLoc variable, so the output files will be saved to $rootFolder\$outsLoc – in this particular example, DataSync\Outputs:

Outputs Folder

If an error occurs when the script is being executed, an error message will be displayed to provide more information about the potential cause of this error. Additionally, a DataOutput_error.txt file with details of the error will be created.

Here’s the script in its entirety:

Full script

If any questions or issues arise during the process of setting this up, feel free to contact us anytime at support@devart.com

Leave a Comment