2010-07-01 119 views
13

我使用這個腳本,想加入2和表3分的條件和更新T1:SQL內連接2臺帶有多列的條件和更新

Update T1 set T1.Inci = T2.Inci 
ON T1.Brands = T2.Brands 
AND T1.Category= T2.Category 
AND T1.Date = T2.Date 

,但我遇到的問題:

Incorrect syntax near the keyword 'ON'

不知道爲什麼。

回答

26
UPDATE 
    T1 
SET 
    T1.Inci = T2.Inci 
FROM 
    T1 
INNER JOIN 
    T2 
ON 
    T1.Brands = T2.Brands 
AND 
    T1.Category= T2.Category 
AND 
    T1.Date = T2.Date 
+4

'ON'和'AND'運算符只驗證平等嗎?他們可以做相當於if((T1.Brands T2.Category))嗎? – 2014-02-08 00:26:06

4

你需要做的

Update table_xpto 
set column_xpto = x.xpto_New 
    ,column2 = x.column2New 
from table_xpto xpto 
    inner join table_xptoNew xptoNew ON xpto.bla = xptoNew.Bla 
where <clause where> 

如果你需要一個更好的答案,你可以給我們更多的信息:)

+0

你可以添加一個鏈接到SQL語法更新。 – 2010-07-01 08:54:20

+0

你是對的 here:http://msdn.microsoft.com/en-us/library/ms177523.aspx – 2010-07-01 09:00:02

3
UPDATE T1,T2 
INNER JOIN T1 ON T1.Brands = T2.Brands 
SET 
T1.Inci = T2.Inci 
WHERE 
    T1.Category= T2.Category 
AND 
    T1.Date = T2.Date 
+0

讀者可以通過內部連接瞭解它。至少是語法方式。 – vikasmcajnu 2015-02-12 17:19:53