oracle的多表插入學習筆記
----------------------------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 ;
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 ;
註意: when語句裏面只能使用pseudocolumn
--------------------conditional FIRST INSERT-----------------------
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 ;
如果有一部分返回行不滿足第一個語句的條件,該部分會被當作條件插入處理。
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的多表插入學習筆記