我得到了一个ORA-30926错误。我对此进行了研究,发现这通常是由USING子句中指定的查询中的重复项造成的。
问题是,我正在错误地移除副本,并将其存储在一个温度表(temp_distinct)中,而这反过来又指在MERGE中。下面是我的代码片段:
MERGE INTO name_test nt
USING (select name from temp_distinct) s
ON (1=1)
WHEN MATCHED
THEN UPDATE SET nt.fn = s.name, nt.LN = s.name这是我的桌子的结构:
NAME_TEST
FN LN
----- -----
Ruc Rag
Ruc Ran
Sam Kum
Ruc Ran
Ruc Kum
Ran Dev
Rag Agar
Rag RanTEMP_DISTINCT
FN NUMB NAME NUM
----- ---- ----- ---
Sam 1 Mark 1
Rag 2 Steve 2
Dev 3 John 3
Kum 4 Dave 4
Ruc 5 Mich 5
Agar 6 Dean 6
Ran 7 Phil 7因此,如您所见,在USING子句中没有重复。我正在尝试取代NT.FN = S.NAME和NT.LN = S.NAME。
基本上,我希望将FN中的名称和NAME_TEST表中的LN替换为与TEMP_DISTINCT表不同的名称。最后的产出如下:
FN LN
------ ------
Mich Steve
Mich Phil
Mark Dave
Mich Phil
Mich Dave
Phil John
Steve Dean
Steve Ran发布于 2014-07-24 16:06:44
除了来自@zaratustra的解释之外,您的合并还试图将fn和ln设置为相同的name值,这样它就不会给出您想要的结果,即使它成功了。在尝试更新fn或ln时,不能在using子句中使用它们。
如果您的name_test表有一个主键(或至少是唯一的)列,那么您可以在合并中包含这个列,但是您仍然只能在一次传递中正确地更新fn或ln值。
我不知道你为什么不做一个简单的更新:
update name_test nt
set fn = (select td.name from temp_distinct td where td.fn = nt.fn),
ln = (select td.name from temp_distinct td where td.fn = nt.ln);
8 rows updated.
select * from name_test;
FN LN
----- -----
Mich Steve
Mich Phil
Mark Dave
Mich Phil
Mich Dave
Phil John
Steve Dean
Steve Phil 发布于 2014-07-24 14:44:43
您的查询无法执行,因为(1 = 1) oracle无法获得一组稳定的行,为了清楚起见,请检查以下内容:
SQL> create table ttt (name varchar2(20 char));
Table created.
SQL> insert into ttt values('first1');
1 row created.
SQL> insert into ttt values('second2');
1 row created.
SQL> merge into ttt t1 using (select 'name' name from dual) t2 on (1 = 1) when matched then update set t1.name = t2.name;
2 rows merged.
SQL> select * from ttt;
NAME
--------------------
name
name"t2“子查询中只有一行,合并操作已经成功完成。但是,如果您的子查询获得多行,我将遇到ORA-30926:
SQL> merge into ttt t1 using (select 'name' || level name from dual connect by rownum < 4) t2 on (1 = 1) when matched then update set t1.name = t2.name;
merge into ttt t1 using (select 'name' || level name from dual connect by rownum < 4) t2 on (1 = 1) when matched then update set t1.name = t2.name
*
ERROR at line 1:
ORA-30926: unable to get a stable set of rows in the source tables
SQL> select * from ttt;
NAME
--------------------
name
name
SQL>Oracle不知道哪些值必须与行相关联。您的结果集不明确。要能够执行此合并,您必须具备正常的条件,例如:
SQL> alter table ttt add id number(10);
Table altered.
SQL> update ttt set name = 'name1', id = rownum;
2 rows updated.
SQL> select * from ttt;
NAME ID
-------------------- ----------
name1 1
name1 2
SQL> merge into ttt t1 using (select 'name' name, level id from dual connect by rownum < 4) t2 on (t1.id = t2.id) when matched then update set t1.name = t2.name;
2 rows merged.
SQL> select * from ttt;
NAME ID
-------------------- ----------
name 1
name 2
SQL>如您所见,"ttt“表已成功合并。
https://stackoverflow.com/questions/24936612
复制相似问题