Click here to Skip to main content
15,867,756 members
Articles / Database Development / SQL Server
Article

Database Schema Comparison Utility

Rate me:
Please Sign up or sign in to vote.
4.74/5 (32 votes)
16 Oct 2006CPOL2 min read 147.2K   4K   133   53
An article describing a way to find the differences between two database instances
Sample Image - DatabaseCompare.jpg

Introduction

When most developers develop database code (including tables, views, stored procedures and functions, they tend to develop in a development environment and then migrate those changes later to other servers (staging, production, whatever). While some of us are good about keeping track of those changes, the majority of us are not.

Red-Gate Software offers a tool, SQL Compare to compare the schema of two databases and show them to the user. This tool is not inexpensive, but it works very well.

This article provides the basic functionality used to do the same thing as SQL Compare, at a fraction of the price.

Background

SQL 2000 and above uses a series of system tables to store the definition of all objects in a database. The important objects (for this project) are:

  • sysobjects - Stores a list of all objects in the database (MSDN Reference)
  • syscolumns - Stores a list of the columns of the objects in the database (MSDN Reference)
  • syscomments - Stores the definition of objects in the database (MSDN Reference)

Using the Code

The code is broken into two projects:

  • DatabaseCompare (the Winforms project)
  • DatabaseCompare.Domain (the database access project)

There is nothing ground breaking in the Winforms project. I did use a couple delegates to make the UI more responsive when performing long tasks.

The Domain project contains classes that actually perform the work in gathering the schema of a database and comparing the two databases.

The code is fairly self explanatory, the main entry points to the Domain code are Database.TestConnection(), Database.LoadObjects(), and Database.CompareTo().

Points of Interest

Longer function/stored procedure/view definitions are maintained in multiple rows in the syscomments database and must be retrieved like so:

C#
using( SqlCommand command = conn.CreateCommand() )
{
    command.CommandText = "select text from syscomments where id=@id";
    command.Parameters.Add( "@id", this.Id );
    using ( SqlDataReader reader = command.ExecuteReader() )
    {
        while( reader.Read() )
            textDefinition += reader.GetString( 0 ).Trim().ToLower();
    }
}

This code does not create drop/create scripts for tables, but that is a fairly trivial exercise for you to do if you need that functionality.

Using the sysdepends table, you could determine what the dependencies for the objects are, in case you are creating CREATE scripts and need to know what to create first.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I'm the owner and principal consultant of a small (one man!) shop here in the Dallas, Texas area.

I mostly work with Microsoft technologies, but run a lot of Linux at home.

Comments and Discussions

 
GeneralDatabase X is configured incorrectly. Please check the configuration Pin
krb_iiaba23-Jul-07 4:04
krb_iiaba23-Jul-07 4:04 
GeneralRe: Database X is configured incorrectly. Please check the configuration Pin
Sean Goodpasture23-Jul-07 6:27
Sean Goodpasture23-Jul-07 6:27 
GeneralThanks! Pin
boomaze1-Jun-07 6:12
boomaze1-Jun-07 6:12 
GeneralAwesome -- Thanks! Pin
KimZander3-Apr-07 7:02
KimZander3-Apr-07 7:02 
GeneralRe: Awesome -- Thanks! Pin
Sean Goodpasture3-Apr-07 16:03
Sean Goodpasture3-Apr-07 16:03 
GeneralSQLDBCompare is better Pin
RayMu200224-Oct-06 3:53
RayMu200224-Oct-06 3:53 
GeneralRe: SQLDBCompare is better Pin
Sean Goodpasture24-Oct-06 4:40
Sean Goodpasture24-Oct-06 4:40 
GeneralType unsupported problem Pin
jaschag24-Oct-06 0:03
jaschag24-Oct-06 0:03 
When asking for a change script I get the following:

-- Table product
-- Type unsupported

What does that mean?
GeneralRe: Type unsupported problem Pin
Sean Goodpasture24-Oct-06 4:38
Sean Goodpasture24-Oct-06 4:38 
GeneralSqlDbDiff Pin
DannSmith23-Oct-06 7:42
DannSmith23-Oct-06 7:42 
GeneralRe: SqlDbDiff Pin
Sean Goodpasture24-Oct-06 4:41
Sean Goodpasture24-Oct-06 4:41 
GeneralINFORMATION_SCHEMA views Pin
Steve Hansen17-Oct-06 2:41
Steve Hansen17-Oct-06 2:41 
GeneralRe: INFORMATION_SCHEMA views Pin
Jeremy Coenen17-Oct-06 2:57
Jeremy Coenen17-Oct-06 2:57 
GeneralRe: INFORMATION_SCHEMA views Pin
Sean Goodpasture17-Oct-06 4:26
Sean Goodpasture17-Oct-06 4:26 
GeneralRe: INFORMATION_SCHEMA views Pin
seesharper17-Oct-06 22:23
seesharper17-Oct-06 22:23 
GeneralRe: INFORMATION_SCHEMA views Pin
Sean Goodpasture18-Oct-06 6:05
Sean Goodpasture18-Oct-06 6:05 
GeneralFree tool Pin
pinx16-Oct-06 23:32
pinx16-Oct-06 23:32 
GeneralRe: Free tool Pin
Jeremy Coenen17-Oct-06 2:53
Jeremy Coenen17-Oct-06 2:53 
GeneralRe: Free tool Pin
Daniel Bass12-Dec-06 23:00
Daniel Bass12-Dec-06 23:00 
GeneralVery good tool Pin
demiansc16-Oct-06 16:09
demiansc16-Oct-06 16:09 
GeneralRe: Very good tool Pin
Sean Goodpasture16-Oct-06 18:44
Sean Goodpasture16-Oct-06 18:44 
GeneralDemo Zip File Pin
Dewey12-Oct-06 22:22
Dewey12-Oct-06 22:22 
GeneralRe: Demo Zip File Pin
Sean Goodpasture16-Oct-06 18:43
Sean Goodpasture16-Oct-06 18:43 
GeneralGreat project! Pin
bvermilion10-Oct-06 12:04
bvermilion10-Oct-06 12:04 
GeneralRe: Great project! Pin
hexy12-Oct-06 16:35
hexy12-Oct-06 16:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.