Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

How do I compare and synchronize two databases in Microsoft SQL Server?

5 August 2020

In this material I will show you how to compare two databases in Microsoft SQL Server with the possibility of further synchronization, while showing a simple way that does not require writing complex instructions in T-SQL.

Why compare and synchronize databases?

I will start with the fact that you can compare and synchronize not only data, but also objects on SQL Server, such as procedures, functions, indexes and so on, ie you can compare the whole scheme, the structure of the database.

In fact, there may be many reasons to compare two databases in Microsoft SQL Server, here are some possible scenarios:

  • Let’s say you have several databases with a similar structure, making changes that are not clearly controlled, such as test bench and industrial databases.
  • Or, you just need to compare and synchronize data from two databases that do not have replication, but the data in one database was made and the data in the other was not made.
  • Another possible situation is that there are several branches, whose databases are accompanied by local developers who can make unregulated changes (add / delete a column in the table, create an index, etc.), and when it comes to the fact that the databases must be absolutely identical, there are serious problems.
  • It is also possible that you have several absolutely identical databases, but for some unknown reasons their performance is different, and in order to find out what the reason is, it is necessary to compare these databases.
  • You may have another reason why you need to compare the data or structure of two databases, it’s possible to do it anyway and I’ll tell you how.

Database comparison and synchronization in Microsoft SQL Server

In order to compare and synchronize databases in Microsoft SQL Server, you need to write certain instructions in T-SQL, which can cause problems, as many simply do not know enough T-SQL.

Even for programmers who are able to do this, it will be quite difficult to implement it due to the labor intensity of such a task. And if such a task is a one-time one, it may even seem senseless to spend so much effort on comparison.

But due to the fact that the task of comparison and synchronization of databases is widespread, it has affected the creation of special automated tools for comparison and synchronization of databases, in other words, you do not need to write complex instructions, because there are special tools that have a convenient and simple interface.

Comparison programs and database synchronization in Microsoft SQL Server

There are not so many tools for comparing and synchronizing databases, and almost all of them are paid. Today I will tell you about Devart software products, namely those designed for comparing SQL Server databases.

They have a low cost compared to the functionality they provide, and most importantly, they have a free full-function trial version for 30 days, which will allow you to test the tools and make a decision about buying. And if you have a one-time task, it turns out that this is an absolutely free for you database comparison tool.

Devart has the following database comparison and synchronization programs in Microsoft SQL Server:

  • dbForge Data Compare for SQL Server is a tool for comparing and synchronizing data in Microsoft SQL Server databases;
  • dbForge Schema Compare for SQL Server is a tool for comparing and synchronizing database schemas in Microsoft SQL Server;
  • dbForge Compare Bundle for SQL Server is a suite of tools that contains the above two programs for complex comparison of databases, while it is cheaper to buy this package.

It should also be noted that these tools allow not only to compare and display the results in the program interface, but also generate reports based on the analysis, and upload them to Excel or HTML format, which, in my opinion, is very convenient.

Today, as an example, I will tell you how to compare data from two Microsoft SQL Server databases, I will use dbForge Compare Bundle for SQL Server, because it includes, as I have already mentioned, both Data Compare and Schema Compare, personally, both tools will be useful to me, so I will download and install Compare Bundle for SQL Server package.

Among other things, this package also includes plug-ins for SQL Server Management Studio, which adds another plus to this package.

Input data for example

To clearly show you how the data comparison process works, I will create two separate test databases. I will add a couple of tables to them and fill them with data that will be slightly different.

Below is a complete guide to creating two databases with data to compare.

--Creation of database 1
CREATE DATABASE TestDB1;
GO
USE TestDB1;
GO
--Creation of the Goods table
CREATE TABLE Goods (
ProductId INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_ProductId PRIMARY KEY,
Category INT NOT NULL DEFAULT (1),
ProductName VARCHAR(100) NOT NULL,
Price MONEY NULL,
);
GO
--Creation of Categories Table
CREATE TABLE Categories (
CategoryId INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_CategoryId PRIMARY KEY,
CategoryName VARCHAR(100) NOT NULL
);
GO
--Adding a foreign key restriction (FOREIGN KEY)
ALTER TABLE Goods ADD CONSTRAINT FK_Category
FOREIGN KEY (Category)
REFERENCES Categories (CategoryId)
ON DELETE SET DEFAULT
ON UPDATING NO ACTION;
GO
--Adding rows to the Categories table
INSERT INTO Categories (CategoryName)
VALUES ("PC Accessories"),
("Cell Phones")
("Household Appliances")
GO
--Adding rows to the Goods table
INSERT INTO Goods(Category, ProductName, Price)
VALUES (1, 'System Block', 300),
(1, 'Monitor', 200),
(2, 'Smartphone', 100);
GO
-------------------------------------------------------------------------
--Database creation 2
CREATE DATABASE TestDB2;
GO
USE TestDB2;
GO
--Creation of the Goods table
CREATE TABLE Goods (
ProductId INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_ProductId PRIMARY KEY,
Category INT NOT NULL DEFAULT (1),
ProductName VARCHAR(100) NOT NULL,
Price MONEY NULL,
);
GO
--Creation of Categories Table
CREATE TABLE Categories (
CategoryId INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_CategoryId PRIMARY KEY,
CategoryName VARCHAR(100) NOT NULL
);
GO
--Adding a foreign key restriction (FOREIGN KEY)
ALTER TABLE Goods ADD CONSTRAINT FK_Category
FOREIGN KEY (Category)
REFERENCES Categories (CategoryId)
ON DELETE SET DEFAULT
ON UPDATING NO ACTION;
GO
--Adding rows to the Categories table
INSERT INTO Categories (CategoryName)
VALUES ("PC Accessories"),
("Cell Phones")
GO
--Adding rows to the Goods table
INSERT INTO Goods(Category, ProductName, Price)
VALUES (1, 'System Block', 300),
(1, 'Monitor', 250);
GO
--data sampling
SELECT * FROM TestDB1.dbo.Goods;
SELECT * FROM TestDB2.dbo.Goods;

As you can see, the data in Goods tables is distinguished by two rows, one row is simply not in the second database, and the second difference is that in the Price column the value is different (in one database 200, in the other 250). In addition, the Categories table also differs in one row.

So, we have the task of finding these differences, it is assumed that we do not know them, and the database is large, there are a lot of tables and data. I created two simple tables just so that you can clearly see these changes so that you can see where you need to look.

Installing dbForge Compare Bundle for SQL Server

First let’s download and install this program, although the installation process of Compare Bundle for SQL Server is not complicated.

Step 1 – Download the program distribution kit

You can download the program on the official website, on the download page, here it is – https://www.devart.com/dbforge/sql/compare-bundle/download.html.

I choose the Professional version, and I click “Download.”

The result will be a file with the size of about 70 megabytes.

Step 2 – Installing Compare Bundle for SQL Server on Windows 10

I will install it on Windows 10.

Start the downloaded file and in the first window click “Install”.

Note! Since the dbForge Compare Bundle for SQL Server toolkit includes extensions for SSMS, SQL Server Management Studio must be closed during the installation.

Installing Compare Bundle for SQL Server on Windows 10

Step 3 – Choosing a catalogue for installation

If necessary, at this step you can change the directory where the program should be installed. Press “NEXT”.

you can change the directory where the program should be installed

Step 4 – Selection of components

Here we leave it as default, because we are offered to select the programs to be installed, we need everything, so click “NEXT”.

Selection of components

Step 5 – Finishing the installation

The installation is completed when a message appears. Press “Finish”.

Finishing the installation

Comparison and synchronization of data using dbForge Data Compare for SQL Server

To start comparing data, run dbForge Data Compare for SQL Server, it can compare: tables, views, database backups and even arbitrary queries.

You can launch the program from the “Start” menu. Also in the SSMS object browser in the context menu of databases there will be new items with which you can run extensions to work with these programs in SQL Server Management Studio environment.

Note!

At startup the program will be every time, before buying a license, we say that it will be valid for 30 days. We just close this window. In case you often need to compare and synchronize databases, I advise you to purchase a license.

Configuring the connection to Microsoft SQL Server

First let’s configure a connection to Microsoft SQL Server, to do this click “New Connection…” in the “Database” menu.

Then in the window that opens, configure the connection, in my case it’s pretty simple, I select the local server, and I specify Windows authentication. Click “OK” (you can click on Test Connection to verify the connection in advance).

Configuring the connection to Microsoft SQL Server

By the way, dbForge Data Compare for SQL Server supports almost all versions of SQL Server since 2000. Here I used Microsoft SQL Server 2017 in the Express edition.

Comparison of databases

To start the data comparison, press the “New Data Comparison…” button.

Next, specify the source database (Source) and the target database (Target) that we will compare.

  • The Source database is the main database that we will be comparing against;
  • The Target Database is the database that you want to compare and synchronize with the Source Database, i.e. make it the same as the Source.

The source database is configured on the left, the target database is configured on the right. To configure it, you need to specify:

  • Type – what we are comparing, by default, the databases;
  • Connection – specify the connection to the server, since the databases may be on different servers;
  • Database – select the desired database.

My source database is TestDB1 and the target database is TestDB2.

Click “Next”.

Comparison of databases

Next, we can specify the necessary options for comparison, click “Next”.

we can specify the necessary options for comparison

The program will analyze available tables in databases and will list them, if some tables do not need to be compared, you can uncheck them. In my test case, I leave everything as there are only two.

Click “Compare”.

The program will analyze available tables in databases

Then the comparison process will start and its duration will depend on the size of the databases. In my case, the comparison lasted no longer than a second.

The result of the comparison will be displayed on the screen.

The top part of the screen will display the list of tables and the summary data for these tables.

The top part of the screen will display the list of tables

To view the details, simply select the desired table and the detailed data will be displayed at the bottom.

There are 4 tabs.

Only in Source – these data are only in the source, in the target database they are not. In my case, if you remember, in the Goods table of the TestDB1 database there is one row more. The result is that we see one more line on this tab.

these data are only in the source, in the target database they are not

Different Records are strings that are different. We had a single value in the Price column, so this tab shows this row and shows the value in both the source and target tables, which is very convenient.

Different Records are strings that are different

Only in Target – these data are only in the target table, i.e. they are not in the source. I have an empty table there, as I have not reproduced such a scenario.

Identical Records are the same rows, i.e. they are absolutely identical in both databases. I have such strings, or rather one line, it is in TestDB1 and TestDB2.

Identical Records

Generating and uploading a report

To generate a report and upload to Excel, for example, click “Comparison -> Generate Comparison Report…”.

Then select the format in which you want to generate the report, you can also specify a file name and a directory for uploading. On the next step, you can check the options, click “Generate” to start.

Generating and uploading a report

Data Synchronization

We have compared the databases, now if necessary we can automatically synchronize these databases, i.e. TestDB2 make it identical to TestDB1.

To do this, we can click on the green arrow at the top of the program or on the toolbar click “Synchronize”.

Data Synchronization

First, we specify the method of script output, we have three ways: open directly in the SQL editor, save to a file, or execute immediately. I will leave it as default, i.e. open it in the same program in a new tab.

Click “Next”.

we specify the method of script output

Then specify the synchronization options, the most necessary ones are selected by default, I will leave it by default, click “Next”.

the most necessary ones are selected by default

The program dbForge Data Compare for SQL Server will analyze these options and warn you about problems if necessary.

In my case, I am warned that the foreign key may not be recovered after synchronization, and that I have the “Reseed identity columns” option selected, which means that the DBCC CHECKIDENT instruction with the RESEED option will be executed to force a new value to the current ID value on the target database, which can cause problems under certain circumstances. I should say right away that I did not have any problems.

Click “Synchronize”.

The program dbForge Data Compare for SQL Server will analyze these options

After that the program will form the necessary script that synchronizes TestDB2 with TestDB1. To execute it, you can press the “Execute” button.

the program will form the necessary script that synchronizes TestDB2 with TestDB1

After that, the two databases will be exactly the same.

That’s all I have, I hope the article was useful and interesting for you, bye!

 
Tags: , , , , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...