An overview of dbForge Schema Compare
Tags: database, sqlserver, productivity, sql
Published: 2022-05-13 17:10:30

The dbForge Schema compare tool is a SQL tool that is used to compare the schema of two databases that exists on one or multiple instances of the SQL Server. 
Recently, one of my company's clients was facing an issue in their application that accessed the archived database. After troubleshooting the issue, we came to know that the archived database was out of sync with the production database. The schema was out of sync because the execution of a few DDL scripts failed, and the schema remained unchanged. We fixed the issue by writing a stored procedure that compares the schema between production and archived databases and provides the list of missing objects.
Later, we learned about the dbForge Schema Compare tool that can perform the comparison of two SQL databases very easily. It provides the following benefits

- Compare the source and destination database and objects. 
- Generate the scripts that can be used to create an object missing  in the source/target database.
- Generate the report which provides the details of the missing objects.

We can compare the following types of objects in the dbForge Schema Compare tool.

- **SQL Server database:** This option is used to compare the database objects of the source and destination database.
- **Backup of SQL Server database:** This option is used to compare the backup of the SQL Server database.
- **Script Folder:** This option compares the source and destination folders containing the script to create database objects.
- **Source control repository:** This option compares two source control repositories that contain the T-SQL database scripts.

In this article, we will learn to compare the objects of two databases. To demonstrate, I have installed SQL Server 2019 and restored two databases named **_statckoverflow2010_** and **_Stackoverflow2010_old_**. The source database is **stackoverflow2010**, and the target database is **stackoverflow2010_old**. We will find the list of objects that are changed/missing in the target database (Stackoverflow2010_old).
First, click on New schema compare located below the menu bar.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izgscvp30ay93yfn24w7.png)

The New schema comparison wizard starts. You can configure the following options:
- Source and target databases which you want to compare.
- Set advance option to customize [sql compare](https://www.devart.com/dbforge/sql/schemacompare/) process.
Schema and table mapping.

## Configure the source and target database.

First, let us configure the source and destination database. In our case, the source database is Stackoverflow2010, and the target database is stackoverflow2010_old.
We compare the databases; therefore, select **Database** from the Type drop-down box. In the Connection drop-down box, you can select the existing connection or create a SQL Server connection on which the source and destination databases are created. We are creating a new connection; therefore, select **Manage**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82cd787ny384y0n3vpwn.png)

Another dialog box named opens. Here you can see the various parameters and configuration options to create a SQL Server database connection. Click on New to create a **New** connection. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikzpv9csyhpk8f2v9y79.png)

In our case, the configuration parameters are specified as follows.

- **Server Name:** Specify the server's hostname on which the SQL Server is installed. In our case, the hostname is Nisarg-PC.
- **Authentication:** select the authentication type. You can use windows authentication or SQL Server authentication. In our demo, we are using Windows authentication.
- **Database:** select the source database. In this demo, the source database is stackoverflow2010.
- **Connection name:** Enter the desired connection name. 

Click Connect. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3tfb6m42nvl05bvkvppz.png)

Back to the connection manager dialog box, you can see the new connection has been created. Following is the screenshot of the Connection Manager dialog box.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/elgv7kzfgeoee25v2e32.png)

Now, similarly, configure the Target connection. In this demo, the target database is Stackoverflow2010_old on the same SQL Server instance. Therefore, we will change the database name in the Target SQL Server connection Manager. The parameters in the dialog box named Database Connection Properties are following:

- **Server Name:** Specify the hostname of the server on which the SQL Server is installed. In our case, the hostname is Nisarg-PC.
- **Authentication:** select the authentication type. You can use windows authentication or SQL Server authentication. In our demo, we are using Windows authentication.
- **Database:** select the source database. In this demo, the target database is stackoverflow2010_old.
- **Connection name:** Enter the desired connection name. 

Click Connect. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lqx7076y0j00qc7f32c4.png)

In the Connection Manager dialog box, you can see the new connection has been created. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8z99ptp6zre1keyf271.png)

The source and target screen look like the following image. Click Next to configure the options.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9w7u3icomhgmkm7036yf.png)

**Schema Mapping**
You can set the correspondence between the source and target schema owners in the schema mapping screen. Suppose you are comparing the two databases. The source database has two schemas that the target database doesn't have. The stackoverflow2010 database has two schemas in this demo, but the stackoverflow2010_old database does not have any schema. The wizard automatically sets <None> in the target schema grid-view on the schema mapping screen.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9z1cepp9sjn4h35icmnt.png)

**Table Mapping**
You can choose the list of the database objects you want to compare on the table mapping screen. Also, you can map columns to compare the data type. We will learn more about it in our next article. If you want to exclude any objects from the mapping list, right-click on the object name and click on **Unmap**.
In our demo, we are comparing all objects.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fihn9czspb87197hcvv4.png)

**Begin schema compare process**
Once mappings are completed, click on Compare to begin the database comparison process. If the Database is large and contains many objects, the process takes a while to complete. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jayoj1vhbs3ciqt60lg7.png)

Once the comparison completes, you can see the object difference shown in the following image. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lrnrw32bq86xk6fotecz.png)

The comparison project provides the following details

- Objects that exist in the only source database.

- Objects that exist in both databases but have different values of parameters.

- Objects that exist in the target database.
The comparison project shows the T-SQL queries which can be used to synchronize/change the source and destination database.
In our demo, the details provided by the schema comparison is following:
**<u>Only in the Source database</u>**
The Stackoverflow2010 database contains two schemas named **UserPosts** and **Users**, which do not exist in the **_Stackoverflow2010_old_** database. The comparison project also generates the **CREATE SCHEMA** statement. See the following image:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkxh6n5kzcw53w6o0prf.png)

**<u>Different in source and destination database</u>**
There are two differences found in the source and destination databases.

- The database recovery model.
     - Stackoverflow2010: SIMPLE recovery model.
     - Stackoverflow2010_old: FULL recovery model.
- An additional column named description is in the comment table.

See the following images for reference:
**<u>Image 1: Database recovery model </u>**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sa40lcx1t040vxk9o17i.png)
**<u>Image 2: Column differences in the comment table</u>**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ph3niicul4pabg2g0vkd.png)

**<u>Only in the Target database</u>**

The Stackoverflow2010_old database contains the tblArchivedPosts table that does not exist in the stackoverflow2010 Database. The comparison project also generates the CREATE TABLE statement that might be used to create a table in the source database.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dcvhhml31wmhho7yfss.png)

As you can see, the schema compare tool has provided detailed information on the differences between the source and target database.

## Summary
The above article explains the process of comparing the SQL databases using dbForge schema compare. In my upcoming articles, we will review the process to compare the backups of the SQL databases.