2014-10-27 45 views
0

我正在嘗試更新tableA中的columnA。 ColumnA的新值從tableB ColumnB中提取,使用列a作爲ID。我正在使用以下查詢,但我無法更新表格。用sql中的另一個表列更新列,同一列是id

update tableA a set columnA = (select b.columnB from tableb b where b.columnC = a.columnA) 
where exists (select * from tableb b where b.columnC = a.columnA) and a.columnD = 'ABC' 

以上查詢我得到的例外「單列子查詢返回多行」

update tableA a set a.columnA = b.columnB from tableb b on a.columnA = b.columnC where a.columnD = 'ABC' 

以上查詢我得到的例外「SQL命令不能正確地結束」

update a set a.columnA = b.columnB from tablea a inner join tableb b on a.columnA=b.columnC where a.columnD = 'ABC' 

以上查詢我得到的例外「SQL命令不能正確地結束」

+0

可能重複[如何更新與另一個表的列值一個表列值?( http://stackoverflow.com/questions/4378847/how-to-update-one-table-column-values-with-another-tables-column-values) – OldProgrammer 2014-10-27 16:45:49

回答

0

我認爲你的問題是你在tableB中有多行匹配(「where b.columnC = a.columnA」)。所以當你告訴Oracle:

set columnA = (
select b.columnB 
from tableb b 
where b.columnC = a.columnA) 
where exists ... 

它在tableB中查找給定鍵值的多行。您將不得不決定如何讓Oracle選擇一個。例如,如果你真的不在乎其多重價值的,你可以這樣做(未經測試):

set columnA = (
select distinct(max(b.columnB)) 
from tableb b 
where b.columnC = a.columnA) 
where exists ... 
相關問題