UPDATE語句在DB2、Oralce、SQL Server三種數據庫中的寫法不盡相同,本文將為您分別舉例說明異同,供您參考,希望對您有所幫助。
Oralce和DB2都支持的語法:
UPDATE A
SET (A1, A2, A3) = (SELECT B1, B2, B3 FROM B WHERE A.ID = B.ID)
MS SQL Server不支持這樣的語法,相對應的寫法為:
UPDATE A
SET A1 = B1, A2 = B2, A3 = B3
FROM A LEFT JOIN B ON A.ID = B.ID
個人感覺MS SQL Server的Update語法功能更為強大。MS SQL Server的寫法:
UPDATE A
SET A1 = B1, A2 = B2, A3 = B3
FROM A, B WHERE A.ID = B.ID
在Oracle和DB2中的寫法就比較麻煩了,如下:
UPDATE A
SET (A1, A2, A3) = (SELECT B1, B2, B3 FROM B WHERE A.ID = B.ID)
WHERE ID IN (SELECT B.ID FROM B WHERE A.ID = B.ID)