合并语句,但仅在* * *行匹配时才更新

时间:2015-02-07 12:52:38

标签: sql oracle

我正在运行merge语句,但我希望只有在一个行完全匹配时才会发生更新。

merge into mergetest using dual on (a = 1)
  when matched then update set b = b+1;

我知道我最后可以包含where条款,但我不知道该怎么做。不允许使用组功能,因此count(*) = 1不起作用。

2 个答案:

答案 0 :(得分:1)

这有用吗?

merge into mergetest
    using (select a, count(*) as cnt
           from mergetest
           where a = 1
           group by a
          ) mt
          on mergetest.a = mt.a and cnt = 1
   when matched then
        update set b = b + 1;

答案 1 :(得分:0)

在PL / SQL中,这可以通过以下方式解决:

select count(*) into l_count from mergetest  where a = 1;

case l_count
  when 0 then
    insert into mergetest  (a,b) values (1,1);
  when 1 then
    update  mergetest  set b = b + 1 where a = 1;
  else
    NULL;
  end case;