Introduction
There are many occasions where we may need to compare the schema between two databases. For example, comparing Development and Production databases against each other, or comparing a database against a SQL Server Database project. Microsoft Visual Studio SQL Data Tools provides functionality that allows us to compare the structure of the database tables, stored procedures, views, and other database objects.
Before getting into this article, I would recommend to read the previous two article,
- Create and Publish SQL Server Database Project With Visual Studio
- Link a SQL Server Database Project to a Git Repository
Create Branch in SQL Server Database Project
- First lets create a new branch in the SQL Server Database Project, so that we can manage the scripts.

Schema Compare feature in Visual Studio
- From the main menu, go to the Tools -> SQL Server sub-menu -> New Schema Comparison

- This will load the SqlSchemaCompare query window and the first thing that needs to be done is to select source and target from their corresponding drop-down lists

- In the upper left corner of the SqlSchemaCompare query window, locate the Select source drop-down list and click on the Select Source item

- The Select Source Schema dialog will be shown and in it, by default, the Database option is selected and under it, click the Select Connection button

- This will bring the new Connect dialog in which there are two tabs
- History
- Browse

- After all information for SQL Server and database are entered, click the Connect button

- Once all information is loaded, the Select Source Schema dialog will be shown again and here the OK button should be clicked in order to load the source data source
- Now lets make the Target Project.


- Select Target from the right upper of the screen or next to the source.
- Since we are using SQL Server Database Project, choose the Project as option. By default, the current SQL Server Project will be shown in the dropdown.
- Click Ok once the project is chosen.

- This is how the initial look. Now we need to click the Compare in order to know the changes

Once the process of comparison is finished, the comparison results will be presented and there are two panels shown:
- The Results panel that contains all compared objects sorted by action for the update process
- The Object Definitions panel that shows the SQL scripts of compared objects from source and target SQL databases

- Now that changes done table is listed in the Solution Explorer

- We can save our Schema by simply applying CTRL + S and can be reused whenever it is required.

- The below picture shows the saved schema compare tool.

Demo
Now lets see few examples, Lets create a new table in the SQL Server and apply the schema compare tool.
CREATE TABLE Details.Exam
(
ExamId INT PRIMARY KEY IDENTITY(1,1),
ExamName NVARCHAR(50) NOT NULL
)

- If we run the schema compare tool, the newly added table will be displayed under Add folder as shown in the above. Then we need to apply the Update button, very next to the compare button.

- The newly created table is into the solution
- Now lets see another example. Lets delete already existing table from the database.
DROP TABLE [Details].[Marks]

- After running the Compare, the deleted table will be displayed in the delete folder. Now if we apply the update process, the table will be deleted from SQL Server Database Project in the Visual Studio.

- The table is removed from the SQL Server Database Project.
- Now let’s change the procedure in the database. That changes done SP will be displayed under the Changes folder.


- Now we can commit the changes to the GitHub, since we have created GitHub repo for the SQL Server Project

Conclusion
In this article, we had a detailed look on the Schema Compare Tool in SQL Server Project. With this simple tool, we can manage the Scripts when working in a large number of team or when multiple developers works in database at the same time. I hope you all found this article much useful. Please share your feedback in the comment section.
Consider reading other SQL articles of Mine
- Link a SQL Server Database Project to a Git Repository
- Create and Publish SQL Server Database Project With Visual Studio
- Customize Azure Data Studio with Dashboard Widgets
- Cycle Clipboard Ring In SSMS – Reuse Copied Items
- Set custom colors to differentiate between environments in SSMS
- Recover unsaved SQL queries in SSMS
- Multiple Backup Files of the SQL Server database with SSMS and T-SQL
- Difference between CURRENT_TIMESTAMP vs GETDATE() vs SYSDATETIME() vs GETUTCDATE() in SQL Server
- Change Schema Name of Table in MS SQL Server
- Identifying object dependencies in SQL Server using SP_DEPENDS
- Select Names Starting With Vowels in MS SQL Server
- Display Line Numbers in a SQL Server Management Studio Query Window (SSMS)