在Oracle 9i之前,执行INSERT语句插入数据时,只能为单个表插入数据。从Oracle 9i开始,使用INSERT语句可以将某张表的数据同时插入到多张表中。语法如下:
INSERT ALL insert_into_clause [value_clause] subquery;
INSERT conditional_insert_clause subquery;
如上所述,insert_into_clause用于指定INSERT字句:value_clause用于指定值字句;subquery用于指定提供数据的子查询;conditional_insert_clause用于指定INSERT条件字句。下面通过示例说明使用INSERT 语句执行多表插入的方法。
一、使用ALL操作符执行多表插入
当使用ALL操作符执行多表插入时,在每个条件字句上都要执行INTO字句后的子查询。示例如下:
SQL>insert all
2 when deptno=10 then into dept10
3 when deptno=20 then into dept20
4 when deptno=30 then into dept30
5 when job='CLERK' then into clerk
6 else into other
7 select * from emp;
已创建19行。
在执行了以上INSERT语句之后,会将部门10的雇员信息插入到dept10表,将部门20的雇员信息插入到dept20表,将部门30的雇员信息插入到dept30,将岗位CLERK的所有雇员插入到clerk,将其他行插入到other表。各表的数据显示如下:
emp表:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7782 CLARK MANAGER 7839 1981-6-9 2450.00 10
7839 KING PRESIDENT 1981-11-17 5000.00 10
7934 MILLER CLERK 7782 1982-1-23 1300.00 10
7566 JONES MANAGER 7839 1981-4-2 2975.00 20
7902 FORD ANALYST 7566 1981-12-3 3000.00 20
7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7900 JAMES CLERK 7698 1981-12-3 950.00 30
7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30
7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
7998 MARRP SALESMAN 7699 1983-8-20 1350.00 1200.00 40
15 rows selected
dept10表:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7782 CLARK MANAGER 7839 1981-6-9 2450.00 10
7839 KING PRESIDENT 1981-11-17 5000.00 10
7934 MILLER CLERK 7782 1982-1-23 1300.00 10
dept20表:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7566 JONES MANAGER 7839 1981-4-2 2975.00 20
7902 FORD ANALYST 7566 1981-12-3 3000.00 20
7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20
dept30表:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7900 JAMES CLERK 7698 1981-12-3 950.00 30
7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30
7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
6 rows selected
clerk表:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7934 MILLER CLERK 7782 1982-1-23 1300.00 10
7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7900 JAMES CLERK 7698 1981-12-3 950.00 30
other表:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7998 MARRP SALESMAN 7699 1983-8-20 1350.00 1200.00 40
二、使用FIRST操作符执行多表插入
当使用FIRST操作符执行多表插入时,如果数据已经满足了先前条件,并且已经被插入到某表,那么该行数据在后续插入中将不会被再
次使用。示例如下:
SQL>insert first
2 when deptno=10 then into dept10
3 when deptno=20 then into dept20
4 when deptno=30 then into dept30
5 when job='CLERK' then into clerk
6 else into other
7 select * from emp;
已创建15行。
各表显示数据如下,emp表不变。
dept10表:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7782 CLARK MANAGER 7839 1981-6-9 2450.00 10
7839 KING PRESIDENT 1981-11-17 5000.00 10
7934 MILLER CLERK 7782 1982-1-23 1300.00 10
dept20表:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7566 JONES MANAGER 7839 1981-4-2 2975.00 20
7902 FORD ANALYST 7566 1981-12-3 3000.00 20
7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
7369 SMITH CLERK 7902 1980-12-17 800.00 20
7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20
dept30表:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
7900 JAMES CLERK 7698 1981-12-3 950.00 30
7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30
7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
6 rows selected
clerk表:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
other表:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7998 MARRP SALESMAN 7699 1983-8-20 1350.00 1200.00 40