Merge into的注意点之ORA-30926: 无法在源表中获得一组稳定的行?

大家可以看看:

http://blog.sina.com.cn/s/blog_5d25646e0100qu17.html

的内容,然后我再补充一点:

新建两张table :test_source和test_target,他们的数据分别如下:                                    
SQL> select * from test_source;
NAME               ID
---------- ----------
P3              74834
P4              74835
luo                 8
P1              74832
P2              74833
zhi                 8
SQL> select name ,id from test_target;
NAME       ID
---------- ----------
P3***           74834
P4***           74835
luo**               8
P1***           74832
P2***           74833
zhi**               8
SQL>  merge into test_target
  2   using test_source
  3   on (test_source.id = test_target.id)
  4   when matched then update set test_target.name = test_source.name
  5  when not matched then insert values(test_source.name,test_source.id);
 using test_source
       *
第 2 行出现错误:
ORA-30926: 无法在源表中获得一组稳定的行

---------------------哈哈,报错了,大家想想为什么呢?-----------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
解答如下:
首先我们要知道merge into存在的意义是什么!!!
      使用merge into  是为了根据匹配条件on(condition)利用table_source 的数据更新合并table_target的数据。
merge into 的内部处理是将table_source 的每一条记录和table_target的每一条记录对比匹配,匹配到符合条件的  记录就会进行修改,匹配不到的话就会insert。如果table_source的匹配列中有重复值的话,等到第二次重复的列值匹配的时候,就会将第一次的update后的值再一次update,就是说合并后的table_target中会丢失在table_source中的记录!!!如果记录丢失的话,两表合并的意义何在?!!因此我们使用merge into要注意:源表匹配列中不能有重复值,否则无法匹配(报错!     )。  
 -----------------------------------------------------------------------------------------------------------------我们可以上面将重复的列值去掉试试:
                                              
                                                                                  SQL> delete from test_source where name = 'zhi';
已删除 1 行。
SQL>  merge into test_target
  2   using test_source
  3   on (test_source.id = test_target.id)
  4   when matched then update set test_target.name = test_source.name
  5  when not matched then insert values(test_source.name,test_source.id);
6 行已合并。
  ---------------这回就何合并成功了。                 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值