0
我有一个表有两列......一个int ID和一个varchar用户名。我们要计算一个表中记录的数量以及它们是否存在于另一个表中。比较跨数据库的两个表
表1是源 表2是DEST
--Clear the table
DELETE FROM dest
--Allow identity insert
SET IDENTITY_INSERT dest ON
--copy the source to the destination
insert into dest(id, userName) select id, userName from source
到目前为止,那么好。我现在的问题是,有些记录现在已经被删除,我们希望找到源和目标之间的差异。所以想象一下,在dest上发生了一些活动,导致它丢失了源中仍存在的一些记录。
--Count number of records in source. The result here is 18247
SELECT COUNT(*) FROM source
--Count number of records in dest. The result here is 18298
SELECT COUNT(*) FROM dest
--There is a difference of 51 records. Correct??
--Now count all records that exist in source, but not in dest. i.e. records deleted from dest.
SELECT COUNT(*) FROM source AS sourceTable WHERE NOT EXISTS(SELECT 1 FROM dest AS destTable WHERE destTable.id=sourceTable.id)
--The result here is 18165 (???)
很明显,哪里不存在查询是不正确的。任何线索,我在这里做错了什么?我只想要属于源中存在的记录的ID列表,但现在不再存在于dest中。
什么是RDBMS?请添加到您的标签请... – 2014-10-08 03:51:23