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 ;
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 ;
注意: 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 ;

原文地址:https://www.cnblogs.com/caibird2005/p/1372161.html