SqlServer怎么比较两个表的字段是否一致

SqlServer怎么比较两个表的字段是否一致,第1张

--2个表比较

DECLARE @SourceTableNameVARCHAR(100)

DECLARE @DestTableName VARCHAR(100)

SET @SourceTableName = 'TableA'

SET @DestTableName = 'TableB'

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

查数据字典

select T.COLUMN_NAME

from ALL_TAB_COLUMNS T

where T.TABLE_NAME = 'A表'

minus

select T.COLUMN_NAME

from ALL_TAB_COLUMNS T

where T.TABLE_NAME = 'B表'

这样就找到了DBF_1比DBF_2多出来的列

要抽数据就直接写:

select 查到的几列 from DBF_1就行了

我给你的sql贴上去跑就行了啊,怎么还不清楚么?


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9897667.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存