insert all 的使用方法

 
 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索引时,多表插入语句不能并行执行;
多表插入语句不支持执行计划稳定性;
多表插入语句中的子查询不能使用序列。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值