使用oracle中的insert all来向多个表插入数据

 insert all结构的作用是想几个表中同时插入数据。

下面是一个简单的例子。
我们先来
使用create select 结构创建两个表,这两个表的结构和scott.emp的结构一模一样。如果你对create select
create table insert_all_test1
as
select empno,ename from scott.emp where 1 = 0

create table insert_all_test2
as
select empno,sal from scott.emp where 1 = 0;

然后我们使用insert all,向这两个表插入数据:
insert all
into insert_all_test1 values(no,name)
into insert_all_test2 values(no,sal)
select empno no,ename name,sal from scott.emp;

这里很简单就像两个表插入了数据,虽然最终插入到表中的数据在列上有所区分,但是插入到这两个表的数据的来源是一样的。

insert all结构还有一个用法就是使用when then选择结构来使对应的数据插入到对应的表中,这其实也很简单。
例如:
insert all
when sal > 2000 then
into insert_all_test1 values(no,name)
when sal < 2500 then
into insert_all_test2 values(no,sal)
select empno no,ename name,sal from scott.emp;
这里使用了sal 作为判断条件来将不同的条件数据插入到不同的表中。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值