2017-06-06 71 views
0

我有三個表:a,b,c。表b是a和c的映射表。我正在嘗試更新表中的一列=表c列。使用映射表更新多個連接的表列

在實現更新,我收到一個錯誤:

[S0001][4104] The multi-part identifier could not be bound

UPDATE table_a 
SET table_a.Sector = table_c.Sector 
FROM table_a 
INNER JOIN table_b 
    ON table_a.business_ID = cast(table_b.business_id as BIGINT) 
INNER JOIN table_c 
    ON table_b.ACARA_SML_ID = table_c.ACARA_SML_ID 
WHERE a.State = 'ABC'; 

我感謝所有幫助。

回答

1

使用UPDATE從SELECT子句直接聲明:

UPDATE table_a SET table_a.Sector = C.Sector 
FROM table_c C 
WHERE EXISTS 
(SELECT 1 FROM table_b B B.ACARA_SML_ID = C.ACARA_SML_ID AND 
       table_a.business_ID = CAST(B.business_id AS BIGINT) 
) AND A.[STATE] = 'ABC' 
+0

這很棒,完美運作。謝謝。 – matoneski

0

在您的WHERE子句中,您使用的是a.State,此處沒有爲任何表設置a作爲表別名。

你可以試試這個查詢使用正確的表的別名:

UPDATE A 
SET Sector = C.Sector 
FROM table_a A 
INNER JOIN table_b B ON A.business_ID = CAST(B.business_id AS BIGINT) 
INNER JOIN table_c C ON B.ACARA_SML_ID = C.ACARA_SML_ID 
WHERE A.[STATE] = 'ABC'; 
+0

感謝您的答覆。在實現原始腳本時,我使用別名,但仍收到相同的錯誤。 – matoneski

+0

你可以將'set'語句更改爲'SET Sector = C.Sector'並嘗試? – Arulkumar

+0

您可以請重新檢查您在查詢中使用的表和列,我相信查詢是正確的,但所引用的列是不正確的 – Azar