假如有两条SQL语句,要对同一张表进行update操作。数据库为Oracle10g。
语句1:update table_name set field1=x1, field2=x2 where id=1
语句2:update table_name set field1=y1, field2=y2 where id=2现在想把这两条语句合为一条,以减少对数据库的操作。请问有何办法?

解决方案 »

  1.   

    把这两条语句拼接起来呗update table_name set field1=x1, field2=x2 where id=1;update table_name set field1=y1, field2=y2 where id=2不过,一般开发中都不这样使用,一条语句更新失败会影响另一条语句。
      

  2.   

    1、如果数据来源于另一张表,可以批量更新。
    update table_name a set (a.field1,a.field2)=(select x1,x2 from b where b.id=a.id)
    and exists(select 1 from b where b.id=a.id)
    2、使用存储过程,数据存入索引表、嵌套表(类似数组),然后批量更新。