Click here to Skip to main content
Click here to Skip to main content
Go to top

Database Schema Comparison Utility

, 16 Oct 2006
Rate this:
Please Sign up or sign in to vote.
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:

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

 
GeneralThat's very useful for a rookie like me PinmemberCnKy22-Sep-13 21:47 
SuggestionBuilt in functionality in Visual Studio now PinmemberDuder8128-Nov-12 10:44 
GeneralMy vote of 5 PinmvpKanasz Robert26-Sep-12 4:59 
Questionproject on datbase schmema Pinmembervenkatrupa3-Sep-12 5:58 
QuestionNice Work...!!! Pinmemberadkalavadia4-Jan-12 3:21 
BugApplication show an error while comparing database Pinmemberdinesh830-Dec-11 20:07 
GeneralMy vote of 5 PinmemberMidax15-Sep-11 4:15 
GeneralMy vote of 5 PinmemberSunasara Imdadhusen16-Feb-11 0:23 
GeneralNice work! PinmemberSunasara Imdadhusen16-Feb-11 0:22 
GeneralGreat Work PinmemberGareth Henry28-Jan-09 9:10 
GeneralVery good article PinmemberAjith R Nair16-Jan-09 0:56 
General[Message Removed] Pinmemberped2ped29-Aug-08 19:07 
GeneralTwo improvements PinmemberZubair Ahmed28-Jul-08 0:17 
GeneralThanks PinmemberGaurang Desai25-Jun-08 10:39 
GeneralThanks Pinmemberjawwadalam22-May-08 22:05 
Generalgetting table info Pinmembertundefama22-May-08 3:38 
GeneralExcellent Job PinmemberPeterHicks25-Apr-08 5:00 
GeneralOne nit to pick Pinmemberblemoine27-Feb-08 13:51 
Generalcross thread operation not valid!! Pinmembermfc_surfer9-Sep-07 2:15 
GeneralRe: cross thread operation not valid!! PinmemberSean Goodpasture9-Sep-07 3:15 
GeneralRe: cross thread operation not valid!! Pinmembermfc_surfer13-Sep-07 1:01 
GeneralRe: cross thread operation not valid!! PinmemberSean Goodpasture13-Sep-07 2:23 
GeneralRe: cross thread operation not valid!! PinmemberAaron3224-Sep-07 10:07 
GeneralRe: cross thread operation not valid!! PinmemberMunissoR27-May-08 23:53 
GeneralDatabase X is configured incorrectly. Please check the configuration Pinmemberkrb_iiaba23-Jul-07 4:04 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 17 Oct 2006
Article Copyright 2006 by Sean Goodpasture
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid