2011-11-09 113 views
0

我有兩個表具有相同的模式,一個只是一個表,數據被批量加載到另一個是我的活表。我想從上傳表格中更新行數不同的活動表格。我認爲我在這裏非常接近,但我的更新會在哪裏發揮作用?更新錶行的列值不同於另一個表

這給了我更新的行實際上有變化我要推到我的直播表:

SELECT USC.* FROM UpdateStagingCustomers AS USC 
JOIN StagingCustomers AS SC 
ON USC.CustomerNumber = SC.CustomerNumber 
AND USC.ManufacturerID = SC.ManufacturerID 
WHERE USC.ManufacturerID=18 AND SC.ManufacturerID = 18 
    AND USC.CustomerNumber IN(
     SELECT CustomerNumber FROM StagingCustomers WHERE ManufacturerID=18 
    ) 
    AND 
    (
    USC.Address1 <> SC.Address1 OR 
    USC.Address2 <> SC.Address2 OR 
    USC.AuthorizedDealer <> SC.AuthorizedDealer OR 
    USC.City <> SC.City 
    ) 
+0

'WHERE USC.ManufacturerID = 18 AND SC.ManufacturerID = 18 AND USC.CustomerNumber IN(SELECT FROM CUSTOMERNUMBER WHERE StagingCustomers ManufacturerID = 18 )' 這是否產生不同的結果不僅僅是USC.ManufacturerID = 18?您正在加入CustomerNumber和ManufacturerID,因此它似乎是多餘的。更多你的問題,你試圖更新哪個表? – cadrell0

+0

補充說,作爲一個安全的警衛,我猜可能不需要。我試圖用UpdateStagingTables的值更新StagingCustomers表格 – Slee

+0

然後David的回答是正確的。 – cadrell0

回答

2

我相信這個SQL語句將做的工作,但你可能要測試它在軋製回交易。

UPDATE StagingCustomers 
SET Address1 = USC.Address1, 
    Address2 = USC.Address2, 
    AuthorizedDealer = USC.AuthorizedDealer, 
    City = USC.City 
FROM UpdateStagingCustomers AS USC 
JOIN StagingCustomers AS SC 
ON USC.CustomerNumber = SC.CustomerNumber 
AND USC.ManufacturerID = SC.ManufacturerID 
WHERE USC.ManufacturerID=18 AND SC.ManufacturerID = 18 
    AND USC.CustomerNumber IN(
     SELECT CustomerNumber FROM StagingCustomers WHERE ManufacturerID=18 
    ) 
    AND 
    (
    USC.Address1 <> SC.Address1 OR 
    USC.Address2 <> SC.Address2 OR 
    USC.AuthorizedDealer <> SC.AuthorizedDealer OR 
    USC.City <> SC.City 
    ) 
+0

不需要;我需要更新StagingCustomers上的where子句? – Slee

+0

SQL足夠智能,它看到您從StagingCustomers中選擇並更新所選記錄。請注意,我認爲'FROM'表與'UPDATE'表相匹配更清楚,但它會產生相同的結果。 – cadrell0

+0

只要FROM子句獲取您在SELECT語句中查找的記錄,同一個FROM子句就可以進入UPDATE,並且在UPDATE之後列出的表中將更改該表。 –

相關問題