Click here to Skip to main content
15,179,246 members
Articles / Database Development / SQL Server
Article
Posted 10 Oct 2006

Stats

135.6K views
3.9K downloads
132 bookmarked

Database Schema Comparison Utility

Rate me:
Please Sign up or sign in to vote.
4.74/5 (32 votes)
16 Oct 2006CPOL2 min read
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)

Share

About the Author

Sean Goodpasture
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

 
GeneralRe: INFORMATION_SCHEMA views Pin
Sean Goodpasture17-Oct-06 5:26
MemberSean Goodpasture17-Oct-06 5:26 
GeneralRe: INFORMATION_SCHEMA views Pin
seesharper17-Oct-06 23:23
Memberseesharper17-Oct-06 23:23 
GeneralRe: INFORMATION_SCHEMA views Pin
Sean Goodpasture18-Oct-06 7:05
MemberSean Goodpasture18-Oct-06 7:05 
GeneralFree tool Pin
pinx17-Oct-06 0:32
Memberpinx17-Oct-06 0:32 
GeneralRe: Free tool Pin
Jeremy Coenen17-Oct-06 3:53
MemberJeremy Coenen17-Oct-06 3:53 
GeneralRe: Free tool Pin
Daniel Bass13-Dec-06 0:00
MemberDaniel Bass13-Dec-06 0:00 
GeneralVery good tool Pin
demiansc16-Oct-06 17:09
Memberdemiansc16-Oct-06 17:09 
GeneralRe: Very good tool Pin
Sean Goodpasture16-Oct-06 19:44
MemberSean Goodpasture16-Oct-06 19:44 
Thanks! Make sure you pretend to work really hard for at least a couple hours on it when your boss is around!

Laugh | :laugh:
GeneralDemo Zip File Pin
Dewey12-Oct-06 23:22
MemberDewey12-Oct-06 23:22 
GeneralRe: Demo Zip File Pin
Sean Goodpasture16-Oct-06 19:43
MemberSean Goodpasture16-Oct-06 19:43 
GeneralGreat project! Pin
bvermilion10-Oct-06 13:04
Memberbvermilion10-Oct-06 13:04 
GeneralRe: Great project! Pin
hexy12-Oct-06 17:35
Memberhexy12-Oct-06 17:35 
GeneralRe: Great project! Pin
Sean Goodpasture13-Oct-06 6:47
MemberSean Goodpasture13-Oct-06 6:47 

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.