2017-03-07 125 views
0

我有兩個完全相同的數據庫,整個數據堆從1到另一個都被引入,無論出於何種原因,似乎沒有標誌指示哪個是遷移記錄。比較兩個數據庫中的多個列數據

即時嘗試要做的是比較每個數據庫中的多個字段,以查看哪些記錄被遷移。

因此,例如,DB1可能在作業表中有10,000個作業,而DB2(舊的)有2,000個作業。我知道那些2000個工作被計入DB1的10,000個工作中,但是如果不比較多個列,我就無法區分這兩個工作,直到我得到正確的數字。

我嘗試以下,但無疾而終,並給我結果0:

SELECT COUNT(*) FROm Job j 
FULL OUTER JOIN [DB2.dbo.job jc ON CAST(j.jobID AS NVARCHAR(20)) = jc.jobTitle 
WHERE j.AMID = jc.amid 
    AND j.ClientID = jc.clientID 
    AND j.personID = jc.ClientID 
    AND j.JobReceivedDate = jc.JobReceivedDate 
    AND j.JobTitle = jc.JobTitle 

任何幫助,在此,將不勝感激。

-----編輯---------------------------------------- -------------------返回

在超過300萬行的代碼如下結果:

SELECT 
    d1.JobTitle, d1.AMID, d1.CLIENTID, d1.PersonID, 
    d2.JobTitle, d2.AMID, d2.CLIENTID, d2.PersonID, 
    [RESULT] = CASE 
    WHEN (d1.AMID = d2.AMID OR (d1.AMID IS NULL AND d2.AMID IS NULL)) 
    AND (d1.CLIENTID = d2.CLIENTID OR (d1.CLIENTID IS NULL AND d2.CLIENTID IS NULL)) 
    AND (d1.PersonID = d2.PersonID OR (d1.PersonID IS NULL AND d2.PersonID IS NULL)) 
    THEN 1 ELSE 0 END 
FROM [db1].dbo.Job AS d1 
FULL OUTER JOIN 
[db2].dbo.Job AS d2 
    ON D1.JobTitle = D2.JobTitle 

只有84000在這個表中的記錄。

-----編輯---------------------------------------- ---------------------

更改我的初始查詢到以下行之有效:

SELECT DISTINCT j.jobID, jc.jobID, jc.JobTitle, j.JobTitle FROM Job j 
FULL OUTER JOIN [Gulf-Jobs].dbo.job jc ON j.JobTitle = jc.JobTitle 
WHERE j.JobTitle = jc.JobTitle 
    AND j.ClientID   = jc.clientID 
    AND j.personID   = jc.personID 
    AND j.JobReceivedDate = jc.JobReceivedDate 
    AND j.JobTitle   = jc.JobTitle 
    AND j.JobUpdatedDate = jc.JobUpdatedDate 
    AND j.JobTypeID   = jc.JobTypeID 
    AND j.JobStatusID  = jc.JobStatusID 
ORDER BY Jc.JobID 
+1

按照此鏈接:http: //stackoverflow.com/questions/14441579/compare-the-value-of-multiple-columns-from-two-different-databases。希望有幫助。 – vanloc

+0

'作業j內部連接DB2.dbo.job上的select count(*)j.AMID = jc.amid AND j.ClientID = jc.clientID AND j.personID = jc.ClientID AND j.JobReceivedDate = jc.JobReceivedDate 和j.JobTitle = jc.JobTitle' 試試這個 – Pream

+0

感謝vanloc,每當我嘗試運行腳本建議它給了我300萬行有隻有84000總表中有 –

回答

1

回答的評論

在您加入的第一個代碼塊(j.jobID AS NVARCHAR(20))= jc.jobTitle中,這就是爲什麼您的計數爲0,但您在第二個代碼塊中加入jobtitle。如果沒有,你的第一個代碼按預期工作,在情況下,如果任何數據爲空請我們inull(列,「」)每列

所以查詢將

SELECT COUNT(*) FROm Job j 
FULL OUTER JOIN [DB2.dbo.job jc ON j.jobTitle = jc.jobTitle 
WHERE j.AMID = jc.amid 
AND j.ClientID = jc.clientID 
AND j.personID = jc.ClientID 
AND j.JobReceivedDate = jc.JobReceivedDate