做这个项目,以前只在业务表增加字段,而没在其历史表增加对应字段
最近客户需要将业务表和历史表的字段保持一致。于是用这个语句来进行对比即可,如果查出来有数据,则表示有字段差异
--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
时间: 2024-10-06 15:21:05