----------------------------multitable Insert--------------------------------
----------------Insert All----------------------------------------------------
SELECT * from EMP;
CREATE TABLE Sal_history (
Empid Number (4), HireDate date, SAL number (7,2)
);
CREATE TABLE Mgr_history (
Empid Number (4), Mgr Number (4), SAL number (7,2)
);
INSERT All
Into Sal_history VALUES (empid,hiredate,sal)
Into Mgr_history VALUES (empid,mgr,sal)
SELECT empno EMPID, HireDate, SAL, MGR
From EMP
WHERE Empno < 7654;
commit;
SELECT * from Sal_history;
SELECT * from Mgr_history;
-------------------Conditional Insert All-------------------------------
TRUNCATE TABLE sal_history;
TRUNCATE TABLE mgr_history;
INSERT All
When Empid >= 7521 then
Into Sal_history VALUES (empid,hiredate,sal)
When Mgr >= 7839 then
Into Mgr_history VALUES (empid,mgr,sal)
SELECT empno EMPID, HireDate, SAL, MGR
From EMP
WHERE Empno < 7654;
commit;
Note: When statements can only use Pseudocolumn
--------------------conditional first INSERT-----------------------
Query The resulting statement, and if the first condition is met, then it will be executed, The returned rows that have already been processed are no longer considered in the following.
INSERT First
when empid < 7654 then
into Sal_history VALUES (empid,hiredate,sal)
when empid = 7499 then
into Sal_history VALUES (empid,hiredate,sal)
else
into Mgr_history values (empid,mgr,sal)
SELECT empno EMPID, HireDate, SAL, Mgr
from EMP
WHERE empno < 7654;
If a portion of the returned row does not satisfy the condition of the first statement, that part is treated as a conditional insert.
INSERT First
When Empid > 7654 Then
Into Sal_history VALUES (empid,hiredate,sal)
When mgr = 7499 Then
Into Sal_history VALUES (empid,hiredate,sal)
Else
into mgr_history values (empid,mgr,sal)
SELECT empno EMPID, HireDate, SAL, MGR
From EMP
WHERE Empno < 7654;
Oracle's Multi-table Insert Learning note