insert all和insert first语句的用法

insert all和insert first语句,用于按给定条件同时向多个表插入数据,以下记录了它们的用法和区别。

1、无条件insert all

用于不分条件的向几个表同时插入一批数据。

建立测试表

create table t1(a number, b varchar2(20));

insert into t1 values(1, 'aaa');

insert into t1 values(2, 'bbb');

insert into t1 values(3, 'ccc');

commit;

create table t2(a number, b varchar2(20));

create table t3(a number, b varchar2(20));

create table t4(a number, b varchar2(20));

从第一个表中获取数据,并同时写入其它几个表,各个表可以有不同的值

insert all into t2 values (a + 1, b)

              into t3 values (a + 2, b)

    select a, b from t1;

commit;

如果各个表插入的数据一样,则以上还可以简化

insert all into t2

              into t3

    select a, b from t1;

commit;

2、有条件insert all

根据查询数据的不同值,分别插入不同表

insert all when a >=1 then

              into t2

              when a >=2 then

              into t3

              else

              into t4

    select a, b from t1;

commit;

观察几个表的查询结果

select * from t1;

A          B

---------- --------------------

1           aaa

2           bbb

3           ccc

select * from t2;

A           B

---------- --------------------

1           aaa

2           bbb

3           ccc

select * from t3;

A           B

---------- --------------------

2           bbb

3           ccc

select * from t4;

未选定行

3、有条件insert first

如果第一个when子句的值为true,对于给定的行执行相应的into子句,并且跳过后面的when子句,后面的插入语句不再执行

insert first when a >=1 then

                 into t2

                 when a >=2 then

                 into t3

                 else

                 into t4

       select a, b from t1;

commit;

观察表的查询结果

select * from t1;

A           B

---------- --------------------

1           aaa

2           bbb

3           ccc

select * from t2;

A           B

---------- --------------------

1           aaa

2           bbb

3           ccc

select * from t3;

未选定行

select * from t4;

未选定行

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28974745/viewspace-2642735/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28974745/viewspace-2642735/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值