ORACLE MERGE INTO语句报错,unable to get a stable set of rows in the source tables

在使用merge into语句,用一张表的数据tab2更新另一张表tab1时,如果tab1中用来匹配的字段一条在tab2中有多条对应的数据,就会报错:

ORA-30926: unable to get a stable set of rows in the source tables

merge into的使用语法

merge into tab1  
using tab2  
on(tab1.id=tab2.id)  
when matched then  
update set tab1.val = tab2.val

解决方法

可以用partition by函数对tab2中的多条字段进行排序,只取第一条来更新tab1。
举个例子,通过学生信息表stu_info来更新学生表student里的学生班级信息。同一个学生对应了不同的班级,使用partition by按创建时间排序,可以得到该学生最新的班级情况。

merge into student a 
using ( 
  select stu_id, class from 
(select t1.*,row_number() over (partition by stu_id order by create_date desc) rn from stu_info t1) t2
where rn=1) b
on (a.stu_id= b.stu_id)
  when matched then update 
 set a.class = b.class;

相关推荐

  1. mysql GROUP BY 语句处理

    2024-02-03 13:04:03       6 阅读
  2. 【Go语言Decodergob: duplicate type received】

    2024-02-03 13:04:03       19 阅读
  3. IDEA

    2024-02-03 13:04:03       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-03 13:04:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-03 13:04:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-03 13:04:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-03 13:04:03       20 阅读

热门阅读

  1. clickhouse批量入库异常日志

    2024-02-03 13:04:03       30 阅读
  2. 前端工程化之:webpack1-11(其他配置)

    2024-02-03 13:04:03       32 阅读
  3. XML要点总结

    2024-02-03 13:04:03       25 阅读
  4. uniapp本地存储的几种方式localStorage

    2024-02-03 13:04:03       35 阅读
  5. 工厂模式与抽象工厂模式

    2024-02-03 13:04:03       26 阅读
  6. uniapp中封装一个svg转base64的组件

    2024-02-03 13:04:03       30 阅读
  7. 构建高效可靠的消息队列系统:设计与实现

    2024-02-03 13:04:03       32 阅读