Compare and auto fix conflicts between two databases T-sql

Columns in old database but not in new , or default value, nullable and data type changed:

SELECT t1.*,
       t2.*
FROM   [OldDb].[INFORMATION_SCHEMA].[COLUMNS] t1
       LEFT JOIN [NewDb].[INFORMATION_SCHEMA].[COLUMNS] t2
         ON t1.COLUMN_NAME = t2.COLUMN_NAME
WHERE  t2.COLUMN_NAME IS NULL
        OR ( t1.TABLE_NAME = t2.TABLE_NAME
             AND t1.TABLE_SCHEMA = t2.TABLE_SCHEMA
             AND t1.COLUMN_NAME = t2.COLUMN_NAME
             AND ( t1.DATA_TYPE != t2.DATA_TYPE
                    OR t1.IS_NULLABLE != t2.IS_NULLABLE
                    OR t1.CHARACTER_MAXIMUM_LENGTH != t2.CHARACTER_MAXIMUM_LENGTH
                    OR t1.COLUMN_DEFAULT != t2.COLUMN_DEFAULT ) )
ORDER  BY t1.COLUMN_NAME 

Columns in new database but not in old :

You can change [OldDb] place with [NewDb] in query

I wrote this question and answer because I've needed this and I've spent so much of time on this.

So I hope everyone can find and use this easily :). I'm looking forward for opinions for make this better.


While nothing wrong with your own approach, I might prefer EXCEPT to grab all the differences here...

;WITH 
 a AS (
    SELECT *
    FROM [OldDB].INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = 'dbo'
      AND TABLE_NAME = 'foo_table'
 )
,b AS (
    SELECT *
    FROM [NewDB].INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = 'dbo'
      AND TABLE_NAME = 'foo_table'
 )
,inanotb AS (
    SELECT * FROM a
    EXCEPT 
    SELECT * FROM b
)
,inbnota AS (
    SELECT * FROM b
    EXCEPT 
    SELECT * FROM a
)
SELECT 'In A not B', *
FROM inanotb
UNION ALL 
SELECT 'In B not A', *
FROM inbnota;