Lets see when we got Error ORA-30926
1. Execute the below scripts:-
CREATE TABLE employee (employee_id NUMBER(5),first_name VARCHAR2(20),last_name VARCHAR2(20),dept_no NUMBER(2),salary NUMBER(10));
INSERT INTO employee VALUES (1, 'Dan', 'Morgan', 10, 100000);
INSERT INTO employee VALUES (2, 'Helen', 'Lofstrom', 20, 100000);
INSERT INTO employee VALUES (3, 'Akiko', 'Toyota', 20, 50000);
INSERT INTO employee VALUES (4, 'Jackie', 'Stough', 20, 40000);
INSERT INTO employee VALUES (5, 'Richard', 'Foote', 20, 70000);
INSERT INTO employee VALUES (6, 'Joe', 'Johnson', 20, 30000);
INSERT INTO employee VALUES (7, 'Clark', 'Urling', 20, 90000);
CREATE TABLE bonuses (employee_id NUMBER, bonus NUMBER DEFAULT 100);
INSERT INTO bonuses (employee_id) VALUES (1);
INSERT INTO bonuses (employee_id) VALUES (2);
INSERT INTO bonuses (employee_id) VALUES (4);
INSERT INTO bonuses (employee_id) VALUES (6);
INSERT INTO bonuses (employee_id) VALUES (7);
COMMIT;
SELECT * FROM employee;
Its has 7 rows for all employees.
SELECT * FROM bonuses;
It has only 1,2,4,6,7 employees.
Run the Below Merge statement :-
MERGE INTO bonuses bUSING (MERGE INTO bonuses bUSING ( SELECT employee_id, salary, dept_no FROM employee) eON (b.employee_id = e.employee_id)WHEN MATCHED THEN UPDATE SET b.bonus = e.salary * 0.1WHEN NOT MATCHED THEN INSERT (b.employee_id, b.bonus) VALUES (e.employee_id, e.salary * 0.05);
7 rows merged.
Now check the target table
SELECT * FROM bonuses;
You will see all the rows from 1 to 7 .
And for matching row the bounus is e.salary * 0.1 (1,2,4,6,7) ,nd for new rows bounus e.salary * 0.05 (3,5)
Now run again the same merge . and it will show 7 Rows merged again .
And the values of bonus column will increase in bonuses table.
And you can run that merge any number of time.
Now test it again ... truncate all both the table and load them again but donot run the merge.
Now add the new row to employees as below
INSERT INTO employee VALUES (1, 'Dan1', 'Morgan', 10, 100000);
Now run merge you will got the error.
ORA-30926: unable to get a stable set of rows in the source tables
Why this time ??Because this time you got two rows in select statement for same employee is i.e 1 , so the merger will not be able to find which row to update so got the error.
" SELECT employee_id, salary, dept_no FROM employee) eON (b.employee_id = e.employee_id"
Now delete the employee_id 1 from bonuses table and run merge again.
delete from bonuses where employee_id=1;
Now run it again , this time it will work and this time 8 rows merged.
This time it worked because this time you will not got any row in select statement for same employee is i.e 1 , so the merger will insert both the rows in the bonus table.
(Note :- Now if you have primary key on employee_id (matching column )in bonus then it will give primary key violation because it will try to insert 2 rows of same employee_id on bonus table)
But if again you run the merge then you will got the error
ORA-30926: unable to get a stable set of rows in the source tables
With the same reason that now employee_id 1 is there in bonuses table and the select statement will give 4 rows and not able to update .
So if to avoid the error “ORA-30926” you should not have more then one rows in target table for same matching columns.