Home Forums SQL Server 2008 T-SQL (SS2K8) compare all tables and find number of common columns between all tables RE: compare all tables and find number of common columns between all tables

  • SELECT t1.name AS list1,

    t2.name AS list2,

    count(distinct c2.name) AS commonColumns

    FROM sys.tables t1

    CROSS JOIN sys.tables t2

    INNER JOIN sys.columns c1 ON c1.object_id = t1.object_id

    LEFT OUTER JOIN sys.columns c2 ON c2.object_id = t2.object_id AND c1.name=c2.name

    GROUP BY t1.name,t2.name

    ORDER BY t1.name,t2.name

    ____________________________________________________

    Deja View - The strange feeling that somewhere, sometime you've optimised this query before

    How to get the best help on a forum

    http://www.sqlservercentral.com/articles/Best+Practices/61537