Oracle sql MERGE INTO with triple where子句

时间:2017-06-19 10:20:40

标签: sql oracle where-clause sql-merge

我有以下SQL代码(这是我到目前为止已经有多少):

MERGE INTO SCHEMA_1.TABLE_1 table1
USING
(

SELECT DISTINCT table2.COLUMN_5, 
    view1.COLUMN_6 

FROM  SCHEMA_2.TABLE_2 table2
LEFT JOIN SCHEMA_2.VIEW_1 view1
ON table2.COLUMN_4 = view1.COLUMN_1
WHERE
   view1.COLUMN_6 is not null
)t2
ON(table1.COLUMN_2 = t2.COLUMN_5)
WHEN MATCHED THEN UPDATE SET
table1.COLUMN_3 = t2.COLUMN_6
where table1.COLUMN_2 in
(
select  

    table1.COLUMN_2
    from 
        SCHEMA_1.TABLE_1 table1, 
    SCHEMA1.TABLE_3 table3, 
    SCHEMA1.TABLE_4 table4,
    SCHEMA1.TABLE_5 table5,
    SCHEMA1.TABLE_6 table6,
    SCHEMA1.TABLE_7 table7
    where 
        table4.COLUMN_7 = table3.COLUMN_8 and
    table5.COLUMN_9 = table4.COLUMN_10 and
    table5.COLUMN_11 = table1.COLUMN_12 and
    table4.COLUMN_13 = table7.COLUMN_14 and
    table7.COLUMN_15 = table6.COLUMN_16 and 
    table6.COLUMN_17 = 'DOL' and
    table4.COLUMN_18 = 1 and 
    table1.COLUMN_2 is not null and 
    table1.COLUMN_3 is null
    order by 
        table1.COLUMN_19 desc
);

但是我收到以下错误消息:

    SQL Error: ORA-30926: unable to get a stable set of rows in the source tables
30926. 00000 -  "unable to get a stable set of rows in the source tables"
*Cause:    A stable set of rows could not be got because of large dml
           activity or a non-deterministic where clause.
*Action:   Remove any non-deterministic where clauses and reissue the dml

导致错误的原因是什么?在哪里更改代码以使其工作?

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

  

“无法在源表中获得稳定的行集”

您收到此错误是因为您的子查询从SCHEMA_1.TABLE_1中选择了您正在合并的表格。我们不能这样做:MERGE INTO SCHEMA_1.TABLE_1 table1语句表示您正在插入或更新该表。那么Oracle应该在子查询中返回哪个版本的数据?

您需要在USING子句中重写子查询,以便捕获子查询的逻辑(显然减去SCHEMA_1.TABLE_1上的选择)。这将为您提供优化ON联接或MATCHED过滤器所需的值。

答案 1 :(得分:0)

合并脚本通常会执行大量的DML操作,我建议您创建第二个查询的视图,而不是在merge语句中使用过滤子句。并且,源语句中也有where子句。

为源和目标创建视图,使合并工作更容易