現在在做的項目資料庫從sqlserver 更改到了oracle所以項目裡所有的sql語句全部要換成支援oralce的樣式。在過程中遇到了這個連表更新。弄了一下午才終於把它搞定了
讓我們來看一下原來的句子:
sqlserver : update table1 set 欄位1=table2.欄位1,欄位2=table2.欄位2,欄位3=table2.欄位3 from (select 欄位4,欄位1,欄位2,欄位3 from table2 wheretime between '" +time1+ "' and '"+time2+"' " group by 欄位4) a where table2.欄位4=table1.欄位4
轉換成oracle文法不支援
然後我嘗試著一個個欄位set 但是沒辦法批次更新 有人說
update customers a -- 使用別名set (city_name,customer_type)=(select b.city_name,b.customer_typefrom tmp_cust_city bwhere b.customer_id=a.customer_id)where exists (select 1from tmp_cust_city bwhere b.customer_id=a.customer_id)
這樣也行 經過嘗試 不可取 原因暫時不明
後來看了個文章說oracle在set的時候只支援1條記錄於是下面是最後成功的句子
oracle : update table1 set (欄位1,欄位2,欄位3)=(select table2.欄位1,table2.欄位2,table2.欄位3 from (select 欄位4,欄位1,欄位2,欄位3 from table2 wheretime between '" +time1+ "' and '"+time2+"' " group by 欄位4) a where table2.欄位4=table1.欄位4)
這樣的話 set語句後的等號只有一條記錄 然而 最裡層的表卻有好幾條資料,這樣的話能夠進行批次更新
方法取自於:http://www.linuxidc.com/Linux/2011-01/31487.htm