insert all
when a1='001' then into a1
when a1='002' then into a2
when a1='003' then into a3
when a1='004' then into a4
when a1='005' then into a5 select * from test;
关于INSERT FIRST和INSERT ALL insert first/all
利用insert first/all使得INSERT语句可以同时插入多张表,还可以根据判断条件来决定每条记录插入到哪张或哪几张表中。
insert first/all语法
[ ALL | FIRST ]
WHEN condition THEN insert_into_clause [values_clause]
[insert_into_clause [values_clause]]...
[WHEN condition THEN insert_into_clause [values_clause]
[insert_into_clause [values_clause]]...
]...
[ELSE insert_into_clause [values_clause]
[insert_into_clause [values_clause]]...
]
insert first/all 是对每一行来进行判断
两者区别:
insert first:对于每一行数据,只插入到第一个when条件成立的表,不继续检查其他条件。
insert all :对于每一行数据,对每一个when条件都进行检查,如果满足条件就执行插入操作。
做一个小试验验证一下:
create table a(a number,b number);
create table b as select * from a;
create table c as select * from a;
SQL> select * from a;
A B
-- ---
1 0
1 1
1 2
2 0
3 0
--测试insert first
执行:
SQL>insert first
when a=1 then
into b
when b=0 then
into c
select * from a;
--检查insert first执行结果
SQL> select * from b;
A B
-- --
1 0
1 1
1 2
SQL> select * from c;
A B
-- ---
2 0
3 0
--清理现场
SQL> delete from b;
SQL> delete from c;
--继续测试insert all
执行:
SQL>insert all
when a=1 then
into b
when b=0 then
into c(a,b)
select * from a;
--检查insert all执行结果
SQL> select * from b;
- ---
1 0
1 1
1 2
SQL> select * from c;
A B
-- ---
1 0
2 0
3 0
多表插入语句的限制条件:
只能对表执行多表插入语句,不能对视图或物化视图执行;
不能对远端表执行多表插入语句;
不能使用表集合表达式;
不能超过999个目标列;
在RAC环境中或目标表是索引组织表或目标表上建有BITMAP索引时,多表插入语句不能并行执行;
多表插入语句不支持执行计划稳定性;
多表插入语句中的子查询不能使用序列。