oracle连表更新

现在在做的项目数据库从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

原文地址:https://www.cnblogs.com/guardianf/p/2837653.html