SQL find the same column in different tables

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 08:48:49

问题


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.


回答1:


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.




回答2:


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)



回答3:


    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



回答4:


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



回答5:


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.



来源:https://stackoverflow.com/questions/6445573/sql-find-the-same-column-in-different-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!