2014-07-24 24 views
0

我得到一個ORA-30926錯誤。我對此進行了研究,發現這通常是由USING子句中指定的查詢中的重複項引起的。ORA-30926 - 合併狀態

問題是我在初步刪除重複項並存儲在temperory表(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 Ran 

TEMP_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.NAMENT.LN = S.NAME

基本上我想在FNLNNAME_TEST表不同的名字從TEMP_DISTINCT替換表名。最後的輸出應該象下面這樣:

FN  LN 
------ ------ 
Mich Steve 
Mich Phil 
Mark Dave 
Mich Phil 
Mich Dave 
Phil John 
Steve Dean 
Steve Ran 

回答

1

除了從@zaratustra的解釋,您的合併還試圖在fnln設置爲相同的name值,因此它不會給它的工作,你想連結果。當您試圖更新它們時,您不能在using子句中使用fnln

如果您name_test表有一個主鍵(或者至少是唯一的)列,那麼你可以包括在合併,但你還是會只能夠在一個通正確更新要麼fnln值。

我不知道爲什麼你沒有做一個簡單的更新:

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 
+0

這是個好主意。根據顯示的輸入,合併操作不是作者需要的。 – zaratustra

+0

非常感謝! !!!!!烏爾的建議解決了我的問題:)謝謝恩 – user3360094

1

您的查詢不能因爲(1 = 1)甲骨文執行不能獲得穩定的組行,要明確檢查了這一點:

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」表已成功合併。

+0

是正確的,,我認爲在ON子句中的名稱將反正是一樣的。但是,@Alex建議直接使用UPDATE而不是MERGE很容易。反正謝謝你的指點:)申請它! ! – user3360094

+0

@ user3360094,我們在這裏解決問題,4個基本運算符(INSERT/UPDATE/DELETE/SELECT)幾乎可以解決您所有的問題,oracle數據庫擁有最強大的SQL語法。順便說一句,我從來沒有用真正的項目合併條款。 – zaratustra