How to compare two SQL Server Databases using SQL Server Data Tools
Table of Content
The new-edge tools have taken over the IT space like a storm. Many tools bring revolutionary changes and one of them is SQL. SQL Server data tools are used to develop and compare different databases. There are many scenarios where an SQL software programmer or database administrator needs to compare two databases. There are many tools available for comparison of schema & data of two databases.
But, if we have SQL Server Data Tools (SSDT), we don’t need any other additional tool. It is like a one-stop-shop. So, in this blog, we will learn everything about SQL server data tools from installation usage to comparison and other essential aspects. Let’s begin with the installation process.
SSDT can be installed as part of Visual Studio Installation. Visual Studio Community Edition also support this.
Comparing Schemas of Two Databases
Following are steps to compare the schema of two databases:
- From Visual Studio, Open Tools 🡪 SQL Server 🡪 New Schema Comparison. It will open a screen for Schema Comparison.
- Click on “Select source”. After clicking on this, You will see a pop-up window. This window will have three options to choose from.
- SQL Server Database Project can be selected. This project file (.sqlproj) should be there in the current solution.
- Databases from Microsoft SQL Server can be selected.
- Data-tier application file (.dacpac) can be selected.
- Select similarly for target schema.
- Click on compare. It will show a comparison result. Here, it will show all types of objects (Table, View, Stored Procedure, Function, etc.) which are not the same in both databases. It will show results by Group of Action:
- Delete: This displays the objects which are already available in the target database not unavailable in the source database. These objects will be deleted from the target schema in the update process.
- Change: Objects are available in both databases, but the schema is different in both databases. These objects will be modified in the target schema during the update process.
- Add: In this process Objects which are available in the server database, but not in the target database. These objects will be created in the target schema during the update process.
We can check/uncheck the objects, those changes will be applied to the Target database.
Next click on “Update” to apply changes to the target database.
Or User can select an option to generate a script.
- Dependencies must be maintained in Object selection. However, SSDT automatically selects relevant objects.
In AdventureWorks Database, EmployeeDepartmentHistory depends on Department, Employee and Shift tables.
Now consider that this table exists in the source schema, but not in the target schema.
- If you have selected the EmployeeDepartmentHistory table only, those 3 tables will be automatically selected. But, checkboxes of these 3 tables will be disabled.
- If all four tables are included in the selection and if you exclude the Employee table, it will disable the checkbox for the Employee table. It means The Employee table is still included. However, once we exclude EmployeeDepartmentHistory, it will enable the checkbox again for the Employee table.
Similar validation will be done for Change or Delete Action objects also.
Disabled checkbox might be shown with indeterminate or checked status based on Action.
- Enable Script option if the target database is available. Not available for SQL Server Database Project or Dacpac file.
- By default, equal objects are not shown in results, but it can be visible by clicking “Show Equal Objects” from the toolbar.
- Objects are default grouped by Action (Delete/Change/Add). It can be changed to Schema or Type (Table/View/Function etc.) from other options in the toolbar.
- Comparison can be saved as a SchemaCompare file (.scmp). Excluded objects will remain excluded while reopening the file.
Compare Data of two databases
Following are steps to compare data of two databases:
- From Visual Studio, Open Tools 🡪 SQL Server 🡪 New Data Comparison. It will open a new wizard screen for Data Comparison.
- Select the Source Database & Target database.
- Select Data Compare Options: Which type of records should be compared:
- Different Records
- Only in Source
- Only in Target
- Identical Records
- By clicking the next button, it will list Tables & Views in the next Screen, which are there in both the tables within the same schema. Here, you can select/unselect tables & views, which will be compared. Also, if you wish to exclude some fields of tables, those fields can be omitted and this will not be compared.Click on Finish to start the comparison.If we choose the Finish option from the first screen from the wizard, it will consider all selections and start the comparison.
- It will show the result for all tables/views.
After the Table name is entered in the first column, the next columns will show Row count for Different Records, Only in Source, Only in Target, Identical Records.Once we select one table from the list, it will show compared data in different tabs of the bottom pane.
First, we need to select the table name from the above list, and then need to select required rows from the bottom pane.
By default, all tables will be selected which have unique records or at least one record in the database. All the records except identical records (which cannot be selected) will be selected by default.
Different records, the primary key column(s) will be displayed a single time, then it will show all columns one by one for source & target database.
In the given Image, For Lineage table, Lineage Key is the primary key, which is displayed only once, then Table Name (Source), Table Name (Target), Data Load Completed (Source), Data Load Completed (Target) so on.
If data is the same, it is shown in Grey Color, while different data is displayed with Black Bold font.
- To complete the whole creation of the database click on Finish selection, then click “Generate Script” or “Update Target” to complete the process.
Note: For both the processes mentioned above has default options. Some options can be changed while others can be compared using a comparison file or from Tools 🡪 Options 🡪 SQL Server Tools in Visual Studio. Also, features may vary in different versions/editions of Visual Studio.
Through this blog, we hope, we were able to elucidate users on Microsoft SQL Server Data tools and their comprehensive usage. This step-by-step guide will help you compare the schema, strings & data of two different databases. It is very helpful for SQL developers & database administrators for managing databases in different situations.