如何在SQL Server数据库中找到哪两个表相同?

时间:2014-09-16 08:58:40

标签: sql sql-server database

我遇到的情况是需要查找具有完全相同列的表。它的数据无关紧要 这是场景。

表1 ANIMAL

SrNo | NAME  
  1  | Giraffe  
  2  | Lion  
  3  | Zebra  

表2:ANIMAL_TEMP

SrNo | NAME  
  1  | DOG  
  2  | CAT  

当我有桌子动画(SrNo,NAME)时如何找到ANIMAL_TEMP(SrNo,NAME)?

1 个答案:

答案 0 :(得分:0)

你可以试试这个吗?

SELECT o.name table_name,
STUFF(
    (
        SELECT ', ' + c.name
        FROM sys.columns c 
        WHERE c.object_id = o.object_id
        ORDER BY c.name
        FOR XML PATH('')
    ),
    1,
    2,
    ''
) as columns
INTO #tmp
FROM sys.objects o 
where o.type = 'U'

select t1.table_name, t2.table_name
FROM #tmp t1
INNER JOIN #tmp t2 ON t1.columns = t2.columns AND t1.table_name <> t2.table_name