Database Table Compare - SQL SERVER

I need to do a (Table to Table) compare in two different databases on the same server.

I need to find the differences between any given set of two tables with the same name in two different databases.   I can not use a third party tool, need a script based solution possibly from the DMVs sysobjects, syscolumns etc....
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Norman MainaCommented:
You can use the tablediff utility.
JGH5Author Commented:
Thanks Norman, However, I need a Schema Compare not a Data Compare.  Need to see if Columns are missing or if the Data Types are different when they should all be the same.
This code will list you the columns, types, etc. in a table.

    FROM sysobjects
    JOIN syscolumns ON =
    JOIN systypes ON syscolumns.xtype=systypes.xtype
and sysobjects.xtype='U'
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Norman MainaCommented:
If you are a developer and have access to VS 2011 ,you can use it to compare and sync schemas.

Or you can use this free tool:
Use sql compare by Redgate. A very helfpul tool.
Scott PletcherSenior DBACommented:
Does that give you what you need, or would you prefer an actual script, as orig stated?
JGH5Author Commented:
An actual script is preferred since I do not have access to any of the mainstream tools.
Do you need more details than in than the script I provided above? You can easily create a tablevar with two lots of fields and fill one lot with the results from one run (on one table) of this code and the other lot with the results from another run (on a different table) of this code. Then select all from the tablevar to see differences immediately
Scott PletcherSenior DBACommented:
Working on a full script now, please just give me a little time :-) .
Go for it Scott! I'm actually in work, so don't have the time for that :)
Scott PletcherSenior DBACommented:
Please try this and let me know what adjustments are needed :-) ::

DECLARE @db1 sysname
DECLARE @table1 sysname
DECLARE @db2 sysname
DECLARE @table2 sysname

SET @db1 = '<db1_name>'          --will default to current db if NULL/''
SET @db2 = '<db2_name>'          --will default to current db if NULL/''
SET @table1 = '<table1_name>'  --will default to @table2 if NULL/''; may include schema. if needed
SET @table2 = NULL                  --will default to @table1 if NULL/''; may include schema. if needed


DECLARE @sql nvarchar(max)
DECLARE @schema1 sysname
DECLARE @schema2 sysname

IF @db1 IS NULL OR @db1 = ''
    SET @db1 = DB_NAME()
IF @table1 IS NULL OR @table1 = ''
    SET @table1 = @table2
IF @db2 IS NULL OR @db2 = ''
   SET @db2 = DB_NAME()
IF @table2 IS NULL OR @table2 = ''
    SET @table2 = @table1
SET @schema1 = PARSENAME(@table1, 2)
SET @table1 = PARSENAME(@table1, 1)
IF @schema1 IS NULL
    SET @schema1 = 'dbo'    
SET @schema2 = PARSENAME(@table2, 2)
SET @table2 = PARSENAME(@table2, 1)
IF @schema2 IS NULL
    SET @schema2 = 'dbo'
--SELECT @db1, @db2, @table1, @table2, @schema1, @schema2

SET @sql = '
    CASE WHEN [Datatype1] <> [Datatype2] THEN ''Yes'' ELSE ''No'' END AS [Datatype Diff?],
    [$db1$?], [$db2$?],
    [Datatype1], [Datatype2],
    [Nullable?], [Identity?], [Computed?]
    COALESCE(, AS ColumnName,    
    CASE WHEN IS NULL THEN ''No'' ELSE ''Yes'' END AS [$db1$?],
    CASE WHEN IS NULL THEN ''No'' ELSE ''Yes'' END AS [$db2$?],
        WHEN LIKE ''%bin%'' OR LIKE ''%char%''  THEN ''('' +
            CASE WHEN t1.max_length = -1 THEN ''MAX'' ELSE CAST(t1.max_length AS varchar(10)) END + '')''
        WHEN IN (''decimal'', ''numeric'')  THEN ''('' +
            CAST(t1.precision AS varchar(10)) + '','' + CAST(t1.scale AS varchar(10)) + '')''
        WHEN IN (''float'', ''real'')  THEN ''('' +
            CAST(t1.precision AS varchar(10)) + '')''
        ELSE ''''
        END, ''-'') AS [Datatype1],
        WHEN LIKE ''%bin%'' OR LIKE ''%char%''  THEN ''('' +
            CASE WHEN t2.max_length = -1 THEN ''MAX'' ELSE CAST(t2.max_length AS varchar(10)) END + '')''
        WHEN IN (''decimal'', ''numeric'')  THEN ''('' +
            CAST(t2.precision AS varchar(10)) + '','' + CAST(t2.scale AS varchar(10)) + '')''
        WHEN IN (''float'', ''real'')  THEN ''('' +
            CAST(t2.precision AS varchar(10)) + '')''
        ELSE ''''
        END, ''-'') AS [Datatype2],
    COALESCE(CAST(c1.is_nullable AS char(1)), ''-'') + '' / '' + COALESCE(CAST(c2.is_nullable AS char(1)), ''-'') AS [Nullable?],
    COALESCE(CAST(c1.is_identity AS char(1)), ''-'') + '' / '' + COALESCE(CAST(c2.is_identity AS char(1)), ''-'') AS [Identity?],
    COALESCE(CAST(c1.is_computed AS char(1)), ''-'') + '' / '' + COALESCE(CAST(c2.is_computed AS char(1)), ''-'') AS [Computed?]    
FROM [$db1$].sys.columns c1
FULL OUTER JOIN [$db2$].sys.columns c2 ON =
INNER JOIN [$db1$].sys.types t1 ON
    t1.system_type_id = c1.system_type_id AND
    t1.user_type_id = c1.user_type_id
INNER JOIN [$db1$].sys.types t2 ON
    t2.system_type_id = c2.system_type_id AND
    t2.user_type_id = c2.user_type_id
    c1.object_id = (SELECT object_id FROM [$db1$].sys.objects o1 WHERE = ''$table1$'' AND o1.schema_id = (
                   (SELECT schema_id FROM [$db1$].sys.schemas s1 WHERE = ''$schema1$''))) AND
    c2.object_id = (SELECT object_id FROM [$db2$].sys.objects o2 WHERE = ''$table2$'' AND o2.schema_id = (
                   (SELECT schema_id FROM [$db2$].sys.schemas s2 WHERE = ''$schema2$'')))
) AS derived

    '$db1$', @db1),
    '$schema1$', @schema1),
    '$table1$', @table1),
    '$db2$', @db2),
    '$schema2$', @schema2),
    '$table2$', @table2)
--PRINT @sql

EXEC (@sql)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JGH5Author Commented:
Thank you Scott; I tested the script and it provides only the differences.  How could this be modified to include the differences plus the rest of the columns in the tables?
Scott PletcherSenior DBACommented:
Hmm, it should list ALL columns from BOTH tables.

Hmm, wait, to be safe, change the INNER JOINs to LEFT OUTER JOINs.  Sorry about that :-) .

It just has columns that tell you if there were differences or not.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.