Comparing and Synchronizing Two Databases |
See Also |
This worked example demonstrates a basic comparison and synchronization of two SQL Server databases.
In the example, the Magic Widget Company has a SQL Server database running on a live Web server. This database contains a number of tables, views, stored procedures, and other database objects. The Magic Widget Company's development team has been working on an upgrade to their Web site. As part of this upgrade, they have made a number of changes to the structure of the database. They have already transferred the changes from the development server to a staging server, but they now need to transfer the changes to the production server.
You can follow the example on your own system. You will need access to a SQL Server to do this.
The worked example uses the following databases:
To create these two databases on your SQL Server:
The databases and their schema are created.
The Project Configuration dialog box is displayed.

In this example, we will compare live databases; you can also compare snapshots that have been created using SQL Compare and scripts folders containing object creation scripts.
If the databases are not displayed in the Database lists, right-click in each Database box and click Refresh, or scroll to the top of the list and click Refresh.
SQL Compare displays a message dialog box that shows the progress of the comparison.
If you select the Close dialog box on completion check box, SQL Compare closes this message dialog box automatically the next time that you run a comparison on a project. For this example, leave the setting as it is.
The comparison results are displayed in the main window.

In this worked example, the comparison results are grouped by:
objects that exist in both databases but are different |
|
objects that exist in WidgetStaging but do not exist in WidgetProduction |
|
objects that exist in both databases and are identical |
To view the objects in a group, click ![]()

You can view a side-by-side, color-coded listing of the differences in the creation SQL, by clicking an object. For example, if you click WidgetPrices, you can see the differences for the table WidgetPrices.

You can print a summary of the comparison results by opening the File menu and clicking Print. You can export the comparison results by opening the Tools menu and clicking Export Comparison Results.
For full details of how to use the comparison results window, see Viewing the Comparison Results.
To synchronize the databases, you first select the objects you want to synchronize. You do this by using the appropriate check boxes in the Include column.
For this worked example, select all the objects that differ.

If you are using SQL Server 2005, a list of SQL Server 2005 object types is also displayed.

Note that you need to do this only for the following object groups:
| objects that exist in both but are different | |
| objects that exist only in (local).WidgetStaging |
All the check boxes in the Include column are now selected.
When you have selected the objects to synchronize, click
Synchronization Wizard.

Synchronization changes will be made to the WidgetProduction database. Confirm the direction in which the changes will be applied by clicking Next.

In this worked example, there are no dependencies. Click Next to generate the synchronization script.

The Summary page displays the following tabs:
In this example, SQL Compare displays a warning to inform you that it cannot use the ALTER TABLE command to change the IDENTITY column, so the synchronization script will rebuild the WidgetReferences table. Warnings are displayed whenever tables require rebuilding as these may be slow operations. Data in tables is preserved when tables are rebuilt.
You can display the synchronization SQL script by clicking View SQL Script. You can then save the script if required.
When you have looked at the script and warnings, click Next to go to the Confirm page.

You can choose either to run the SQL script from within SQL Compare or to launch your SQL application so that you can review the script.
In this example, we will choose to run the script, then compare the databases again to check the results, and save a copy of the synchronization script (the defaults).
Click Finish to run the synchronization.
A confirmation dialog box is displayed. Click Synchronize Now to continue.
SQL Compare displays a message dialog box that shows the progress of the synchronization.
If you select the Close message box on completion check box, SQL Compare closes this message dialog box automatically the next time that you run a comparison on a project or synchronize databases. For this example, leave the setting as it is.
Click OK to close the message box.
SQL Compare then re-compares the databases, and a message dialog box shows the progress of the comparison. Click OK.
The databases are compared and the results are shown in the main window. In this example, all objects are shown to be identical, confirming that the synchronization has been a success.

| See Also |
Using SQL Scripts as a Data Source | Working with Projects | Setting Data Sources | Setting Project Options | Viewing the Comparison Results | Understanding the Comparison Results | Setting Up the Synchronization | Using the Synchronization Wizard | Understanding the Synchronization
© Red Gate Software Ltd 2007. All Rights Reserved.