1、 如果在d1表中有一个字段有多个,并且在使用merge是使用该字段关联会产生ORA-30926的错误,如在上一节面我们使用相关联的字段是deptno,如果在d1中出现多行deptno的一样就会报错。 --构造实验环境(延续上一节的d1、d2表)SQL update d1 set deptno = 10
1、如果在d1表中有一个字段有多个,并且在使用merge是使用该字段关联会产生ORA-30926的错误,如在上一节面我们使用相关联的字段是deptno,如果在d1中出现多行deptno的值一样就会报错。
--构造实验环境(延续上一节的d1、d2表) SQL> update d1 set deptno = 10 where deptno = 30; 1 row updated. SQL> select * from d1; DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 10 SALES CHICAGO 40 OPERATIONS BOSTON
MERGE INTO d2 USING d1 ON (d1.deptno = d2.deptno) WHEN MATCHED THEN UPDATE SET d2.loc = d1.loc || '...'; USING d1 * ERROR at line 2: ORA-30926: unable to get a stable set of rows in the source tables
3、如果将d1和d2倒过来(使用d2的记录去更新d2的记录)就不会发生ORA-30926,原因是在d2中的deptno的值是唯一。
MERGE INTO d1 USING d2 ON (d1.deptno = d2.deptno) WHEN MATCHED THEN UPDATE set d1.loc = d2.loc || '...' SQL> select * from d1; DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING FU JIAN... 20 RESEARCH DALLAS 10 SALES FU JIAN... 40 OPERATIONS BOSTON
--环境 SQL> select * from d1; DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON SQL> select * from d2; DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING FU JIAN 30 SALES CHICAGO --错误示例 MERGE INTO d2 USING d1 ON (d1.deptno = d2.deptno) WHEN MATCHED THEN UPDATE SET d2.loc = d1.loc || '...' DELETE WHERE (d2.deptno = 10) WHERE d1.deptno = 10; WHERE d1.deptno = 10 * ERROR at line 7: ORA-00933: SQL command not properly ended --正确示例 MERGE INTO d2 USING d1 ON (d1.deptno = d2.deptno) WHEN MATCHED THEN UPDATE SET d2.loc = d1.loc || '...' WHERE d1.deptno = 10 DELETE WHERE (d2.deptno = 10);
SQL> select * from d1; DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON SQL> select * from d2; DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING FU JIAN 30 SALES CHICAGO --DELETE WHERE (d1.deptno = 10) MERGE INTO d2 USING d1 ON (d1.deptno = d2.deptno) WHEN MATCHED THEN UPDATE SET d2.loc = d1.loc || '...' DELETE WHERE (d1.deptno = 10) SQL> select * from d2; DEPTNO DNAME LOC ---------- -------------- ------------- 30 SALES CHICAGO... SQL> rollback; Rollback complete. --使用DELETE WHERE (d2.deptno = 10) MERGE INTO d2 USING d1 ON (d1.deptno = d2.deptno) WHEN MATCHED THEN UPDATE SET d2.loc = d1.loc || '...' DELETE WHERE (d2.deptno = 10) SQL> select * from d2; DEPTNO DNAME LOC ---------- -------------- ------------- 30 SALES CHICAGO...
SQL> select * from d1; no rows selected SQL> select * from d2; DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING FU JIAN 30 SALES CHICAGO MERGE INTO d2 USING d1 ON (d1.deptno = d2.deptno) WHEN MATCHED THEN UPDATE SET d2.loc = 'AAAAA' WHEN NOT MATCHED THEN INSERT VALUES(20, 'aaaaa', 'AAAAA') 0 rows merged. SQL> select * from d2; DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING FU JIAN 30 SALES CHICAGO
MERGE INTO d2 USING (SELECT COUNT(*) CNT FROM d1) d ON (d.cnt 0) WHEN MATCHED THEN UPDATE SET d2.loc = 'AAAAA' WHEN NOT MATCHED THEN INSERT VALUES(20, 'aaaaa', 'AAAAA') SQL> select * from d2; DEPTNO DNAME LOC ---------- -------------- ------------- 10 ACCOUNTING FU JIAN 30 SALES CHICAGO 20 aaaaa AAAAA