zmaster
zmaster

Reputation: 421

SQL find the same column in different tables

I have 2 very large tables. I try to figure out what they have in common.

They do not have the same numbers of columns. I could go about to just look at each column name from each table and compare - but they both have hundreds of columns (I have to do it for many such tables).

I use MS Sql server.

There are no constrains and no foregin keys on any of them.

How do I go about doing that ?

Something like this:

select * AS "RES" from Table1 where RES IN (select * column from Table2)

Thanks in advance.

Upvotes: 1

Views: 37555

Answers (5)

Vikram Singh
Vikram Singh

Reputation: 124

    DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX),    @Table1 AS NVARCHAR(MAX)='Table1'    , @Table2 AS NVARCHAR(MAX)='Table2'

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(A.COLUMN_NAME) 
                    from INFORMATION_SCHEMA.COLUMNS A
                        join INFORMATION_SCHEMA.COLUMNS B
                          on A.COLUMN_NAME = B.COLUMN_NAME
                        where A.TABLE_NAME = @Table1
                          and B.TABLE_NAME = @Table2  and A.COLUMN_NAME not in ('Doc','CreatedBy')
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT ' + @cols + ' 
            from 
            (select A.COLUMN_NAME
            from INFORMATION_SCHEMA.COLUMNS A
            join INFORMATION_SCHEMA.COLUMNS B
              on A.COLUMN_NAME = B.COLUMN_NAME
            where A.TABLE_NAME = '''+@Table1+'''
              and B.TABLE_NAME = '''+@Table2+''' 

            ) x
            pivot 
            (
                Max(COLUMN_NAME)
                for COLUMN_NAME in (' + @cols + ')
            ) p '

execute sp_executesql @query

Upvotes: 1

Jason
Jason

Reputation: 421

Assuming your RDBMS supports digests, you could calculate the digest of each row and join on the digest. Something like:

SELECT T1.*
FROM
    (SELECT *, MD5(col1, col2,...) as digest
     FROM Table1) T1,
    (SELECT *, MD5(col1, col2,...) as digest
     FROM Table2) T2
WHERE T1.digest = T2.digest

I'm assuming that the two tables have the same columns and column types.

Upvotes: 0

Jesse Hofmann
Jesse Hofmann

Reputation: 182

If you're looking for column names which are the same between two tables, this should work:

select name from syscolumns sc1 where id = object_id('table1') and exists(select 1 from syscolumns sc2 where sc2.name = sc1.name and sc2.id = object_id('table2'))

You could also make sure they're the same type by tossing in and sc1.xtype = sc2.xtype in the subquery.

Upvotes: 10

itslittlejohn
itslittlejohn

Reputation: 1878

Here is an SP to find common columns in two different tables..

Works in SQL Server

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE GetColumnsData(@T_NAME1 varchar,@T_NAME2 varchar)
AS
BEGIN
DECLARE @Co int;
SET @co = 0;
CREATE TABLE #TEMP_TABLE(C_NAME VARCHAR(50),D_TYPE VARCHAR(50),T_NAME VARCHAR(50));
INSERT INTO #TEMP_TABLE (C_NAME,D_TYPE,T_NAME)( SELECT COLUMN_NAME,DATA_TYPE,
TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @T_NAME1 OR
 TABLE_NAME= @T_NAME2);
SELECT @Co = COUNT(*) from #TEMP_TABLE t , #TEMP_TABLE t1 WHERE t1.C_NAME = t.C_NAME
 and t.D_TYPE = t1.D_TYPE and t.T_NAME != t1.T_NAME
PRINT @co
END

Upvotes: 0

Adriano Carneiro
Adriano Carneiro

Reputation: 58595

If I understood correctly, you are trying to compare the data in the two tables and check what the data has in common.

Provided that you have the columns you want to use for comparison (Table1.YourColumn and Table2.OtherColumn, in the example), you can do this:

select YourColumn from Table1 t1
where exists (select OtherColumn 
              from Table2 t2 
              where t2.OtherColumn = t1.YourColumn)

Upvotes: 1

Related Questions