【问题标题】:Error: ORA-30926: unable to get a stable set of rows in the source tables错误:ORA-30926:无法在源表中获得一组稳定的行
【发布时间】:2013-11-24 22:13:32
【问题描述】:

我收到错误消息:当我运行以下语句时,无法在源表中获得一组稳定的行。

   merge into table_1 c
    using (select rep_nbr, T_nbr, SF from table_2) b
    on (c.rep_id=b.rep_nbr)
    when matched then
    update set 
    c.T_ID =b.T_nbr,
    c.SF=b.SF
    when not matched then
    insert(T_id, SF)
    values(null, null);

当我把 distinct 放在 rep_nbr 之前

merge into table_1 c
    using (select distinct rep_nbr,t_nbr,SF from table_2) b
    on (c.rep_id=b.rep_nbr)
    when matched then
    update set 
    c.T_ID =b.T_nbr,
    c.SF=b.SF
    when not matched then
    insert(T_id, SF)
    values(null, null);

ORA-01400: 无法将 NULL 插入 c.rep_id。 我不想填充 c.rep_id,我只需要它们匹配,然后更新那些匹配的记录。

【问题讨论】:

  • 那么是什么阻止您删除查询的最后一段?当不匹配时......
  • @mihai 我想没有必要拥有那部分。把它拿出来会有什么不同吗?
  • 如果您只需要更新是的​​。在您的最后一个查询中,问题可能是在非空列中插入空值。
  • @mihai 我把它拿出来了,现在它说无法获得稳定的行集
  • @mihai 无论有没有不同,我都会收到相同的消息。

标签: sql plsql


【解决方案1】:

如果我理解正确,您不需要 MERGE,只需一个 UPDATE。像这样的东西应该可以工作:

UPDATE table_1 t1 
SET t1.t_id = (SELECT DISTINCT t2.t_nbr FROM table_2 t2 WHERE t1.rep_id = t2.rep_nbr),
    t1.sf = (SELECT DISTINCT t2.sf FROM table_2 t2 WHERE t1.rep_id = t2.rep_nbr),
WHERE t1.rep_id IN (SELECT rep_nbr FROM table_2)

但是,但是,在更高版本的 Oracle(从 10th 或 11th 开始)中,您可以取出部分合并,例如取出 WHEN NOT MATCHED 部分。

【讨论】:

    【解决方案2】:

    删除部分“不匹配”

    merge into table_1 c
        using (select distinct rep_nbr,t_nbr,SF from table_2) b
        on (c.rep_id=b.rep_nbr)
        when matched then
        update set 
        c.T_ID =b.T_nbr,
        c.SF=b.SF
    

    【讨论】:

      猜你喜欢
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 2021-08-05
      相关资源
      最近更新 更多