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