I have two tables Emp & Dept.
SQL> select * from emp where rownum<4;
EMPNO ENAME JOB MGR SAL DEPTNO
---------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 800
7499 ALLEN SALESMAN 7698 1600
7521 WARD SALESMAN 7698 1250
SQL> select * from dept;
DEPTNO DNAME LOC EMPNO
---------- --------------- --------------- ----------
10 ACCOUNTING NEW YORK 7369
20 RESEARCH DALLAS 7499
30 SALES CHICAGO 7521
40 OPERATIONS BOSTON
i want to update deptno of emp which should be same as deptno of dept table & where empno of dept should be equal to empno of emp; In short i want to update dept using inner join operation with emp;
In Oracle, the canonical way to do it is the
MERGEstatement:However the above will not work if you are using Oracle 9 (see below), or if there are domain index on your tables (Oracle Text for instance)
I asked a question previously about what MERGE statements do.
It is necessary that each row of
ebe connected by the join condition (ONsection) to none or exactly 1 row ofd, otherwise you’ll get ORA-30926 “Unable to get a stable set of rows in the source tables”.Rows of
enot connected to any department get unchanged, rows ofdnot connected to any employee could be used forINSERTS(and in Oracle 9 must be used in aWHEN NOT MATCHED THEN INSERT(cols) VALUES(...)clause; if you’re using Oracle 9 then this answer is not suitable for you).Another option, but subject to the same underlying restrictions:
Or
As you can see solutions are numerous but subject to constraints.