有没有一种简单的方法可以比较SQL Server中两个表之间的列名?

时间:2018-06-27 20:11:37

标签: sql sql-server

我正在使用SQL Server中两个非常广泛,非常相似的表。一个表中存在5-10列中的任何位置,而另一表中则不存在。有没有一种简单的方法可以找出一个表中存在哪些列,而另一个表中不存在?

1 个答案:

答案 0 :(得分:3)

使用information_schema.columns。这是使用full outer join的一种方法:

select c1.column_name, c2.column_name
from (select c.*
      from information_schema.columns
      where table_name = @table1 and table_schema = @schema1
     ) c1 full outer join
     (select c.*
      from information_schema.columns
      where table_name = @table2 and table_schema = @schema2
     ) c2
     on c1.column_name = c2.column_name
where c1.column_name is null or c2.column_name is null