码迷,mamicode.com
首页 > 其他好文 > 详细

如何对比两个表字段差异

时间:2016-01-13 17:38:08      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

做这个项目,以前只在业务表增加字段,而没在其历史表增加对应字段

最近客户需要将业务表和历史表的字段保持一致。于是用这个语句来进行对比即可,如果查出来有数据,则表示有字段差异

--2个表比较
DECLARE @SourceTableName        VARCHAR(100)
DECLARE @DestTableName          VARCHAR(100)
 
SET @SourceTableName = ‘表名1‘
SET @DestTableName = ‘表名2‘
SELECT A.TABLE_NAME AS SourceTable
      ,A.COLUMN_NAME AS SourceColumn
      ,A.DATA_TYPE AS SourceType
      ,B.TABLE_NAME AS DestTable
      ,B.COLUMN_NAME AS DestColumn
      ,B.DATA_TYPE AS DestType
FROM (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @SourceTableName) A
FULL OUTER JOIN (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @DestTableName) B
     ON A.COLUMN_NAME = B.COLUMN_NAME AND A.DATA_TYPE = B.DATA_TYPE
WHERE A.TABLE_NAME IS NULL OR B.TABLE_NAME IS NULL

如何对比两个表字段差异

标签:

原文地址:http://www.cnblogs.com/BigDBrother/p/5127579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!