从外部应用程序升级的最佳方式

时间:2016-05-09 10:02:58

标签: sql oracle oracle11gr2

我有一个使用批量插入将数据放入Oracle数据库表的应用程序。现在我需要改变它,所以它会把它包括起来,但是很难找到最好的解决方案来做到这一点。所有站点建议使用MERGE来插入/更新行,因为它是最有效和最简单的解决方案,但所有示例都基于表中已有的数据。

这种变化的最佳解决方案是什么?是否需要使用某种临时表或临时表,或者有没有办法跳过这个?

1 个答案:

答案 0 :(得分:2)

MERGE也可以与常量值一起使用。

然而,由于Oracle缺乏对values()行构造函数的支持,这有点难看:

merge into the_table
using (
  select 1 as id, 'arthur' as name from dual
) t on (t.id = the_table.id)
when matched then 
   update set name = t.name
when not matched then 
   insert (id, name)
   values (t.id, t.name);

这也可以用于多行:

merge into the_table
using (
  select 1 as id, 'arthur' as name from dual -- first row
  union all
  select 2, 'ford' from dual -- second row
  union all
  select 3, 'zaphod' from dual -- third row
) t on (t.id = the_table.id)
when matched then 
   update set name = t.name
when not matched then 
   insert (id, name)
   values (t.id, t.name);
相关问题