Write for Us
Comparing two databases schemas and examining the differences
Once the project is setup, it can be used for maintaining the schema of the database and applying changes to any environment with or without examining the differences. Usually every change will be done through the project and we can later deploy changes to relevant environments. The comparison and deployment (changes) facilities are available under the "Data" menu with an item called "New Schema Compare". When comparing the schema in the project with another database, the project itself can be considered as either a source or target. If you need to get updates from another database and get them applied to the project created, the project should be treated as the target. If you want apply the changes you have made to the schema in the project to the different database (usually the case), then the project becomes the source. Though we have not made any changes to the project yet, let's compare the project with "AdventureWorks" database and see the procedure of comparing.
Make sure that "Project" radio button is selected under "Source Schema" and "Database" is selected under "Target Schema".
The first section of the result window shows all the available items in the both source and the target, categorized by SQL server objects. First column shows the status of the object . Status can be seen are; Equal: tells that the object has no differences between the source and target. Different: tells that the object has some differences between the source and the target (eg: a data type of the column) New: tells that the object is not available in the target. Missing: tells that object is not available in the source. Second column shows the name of the object of the "Source" and third column shows the action need to be performed against the "Target". There are four types of actions can be performed against the target; Skip: instructs not to do anything related to the object on the target. Create: instructs to create the object in the target. Update: instructs to update the object in the target. Drop: instructs to drop the object in the target. The forth column represents the object in the "Target". The latter part shows the definition of the selected object. It allows us to easily find the differences between the source and the target. In addition to these screen, we can view the "update script" too. It is the script used for updating the target. Since the comparison did not return anything to update, there is nothing much in this script now but it is worth to have a look on it because it generates different types of scripts for different types of implementation of changes. Find "Data -> Schema Compare -> Show Schema Update Script" menu and click it. It opens the "schema update script". Some of the interesting things you can find are; It creates #tmpErrors table to track errors and roll back transactions.
This script can be used to directly update the target database. The "Write Updates" button in the "Schema Compare" toolbar does it too. If you do not want to directly update the target but export the script onto editor, you can use "Export to Editor" button. Note that the comparison has no changes discovered, hence these two buttons are disabled. Simple modification and "update script" Let's do a simple few modifications to the schema and check the result of comparison. Let's add a new column to "Person.Contact" table and change the stored procedure "dbo.uspGetBillOfMeterials". Follow the steps given below;
... [PasswordSalt] [varchar] (10) NOT NULL, [AdditionalContactInfo] [xml] (CONTENT [Person].[AdditionalContactInfoSchemaCollection]) NULL, [rowguid] [uniqueidentifier] NOT NULL ROWGUIDCOL, [ModifiedDate] [datetime] NOT NULL, [NewColumn] int NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO ...
CREATE PROCEDURE [dbo].[uspGetBillOfMaterials] @StartProductID [int], @CheckDate [datetime] AS BEGIN
SET NOCOUNT ON;
-- Test comment
-- Use recursive query to generate a multi-level Bill of Material (i.e. all level 1 -- components of a level 0 assembly, all level 2 components of a level 1 assembly) -- The CheckDate eliminates any components that are no longer used in the product on this date. WITH [BOM_cte]([ProductAssemblyID], [ComponentID], [ComponentDesc], [PerAssemblyQty], [StandardCost], [ListPrice], [BOMLevel], [RecursionLevel]) -- CTE name and columns AS (
...
Now we have done two changes. Let's compare the project with "AdventureWorks" database and see how the differences going to be shown.
Different "update script" for same modification
Data Dude generates the script according to the changes that have been implemented. Remember, we added a column to the table as the last column and it has been interpreted in the "update script" with "ALTER TABLE" statement. But if the same column is added in between two existing columns, the "update script" is bit different. Let's see it.
This is how the "update script" is generated if the column is introduced between an existing columns. You may see some performance problems when updating the target, if the table contains lot of records and heavily indexed. If the table is a large table, avoid adding columns in between existing columns.
Not only these simple changes, Data Dude addresses almost all object changes in the SQL Server database. Since it supports for versioning too, this is the most relevant tool for DBAs for maintaining the schema of databases in multiple environments. If you have not started using it, download the latest from here. You can download power tools for it from here.
The feature "Schema Comparison" is a rich tool for comparing and generating an "update script" that comes with Data Dude. I hope this article provides a good view of "Schema Comparison" and the reasons for using it. Data Dude not only has the schema comparison feature it also has a data comparison feature as well. We will explore this feature in my next article. Here are some of the links that you can use to learn more about Visual Studio Team Edition for Database Professionals.