KingbaseES insert all/first 功能介绍

KingbaseES 内置了对于insert all / first 语法的支持。

一、数据准备

create table t1(product_id number, product_name varchar2(80),month number);  
create table t2(product_id number, product_name varchar2(80),month number);  
create table t3(product_id number, product_name varchar2(80),month number);  
create table t4(product_id number, product_name varchar2(80),month number);  
create table t5(product_id number, product_name varchar2(80),month number);  

insert into t1 values(111, 'apple',1);  
insert into t1 values(222, 'orange',1);  
insert into t1 values(333, 'banana',1);  

insert all  
    into t2  values (product_id, product_name,month)  
    into t2  values (product_id, product_name,month+1)  
    into t2  values (product_id, product_name,month+2)  
    into t2  values (product_id, product_name,month+3)   
select product_id, product_name, month from t1;

test=# select * from t1;
 product_id | product_name | month 
------------+--------------+-------
        111 | apple        |     1
        222 | orange       |     1
        333 | banana       |     1
(3 rows)

test=# select * from t2;
 product_id | product_name | month 
------------+--------------+-------
        111 | apple        |     1
        222 | orange       |     1
        333 | banana       |     1
        111 | apple        |     2
        222 | orange       |     2
        333 | banana       |     2
        111 | apple        |     3
        222 | orange       |     3
        333 | banana       |     3
        111 | apple        |     4
        222 | orange       |     4
        333 | banana       |     4
(12 rows)

二、insert all

insert all 
when month=1 then into t3
when month=2 then into t4
else into t5
select product_id,product_name,month from t2;

test=# select * from t3;
 product_id | product_name | month 
------------+--------------+-------
        111 | apple        |     1
        222 | orange       |     1
        333 | banana       |     1
(3 rows)

test=# select * from t4;
 product_id | product_name | month 
------------+--------------+-------
        111 | apple        |     2
        222 | orange       |     2
        333 | banana       |     2
(3 rows)

test=# select * from t5;
 product_id | product_name | month 
------------+--------------+-------
        111 | apple        |     3
        222 | orange       |     3
        333 | banana       |     3
        111 | apple        |     4
        222 | orange       |     4
        333 | banana       |     4
(6 rows)

三、insert first

truncate table t3;
truncate table t4;
truncate table t5;

insert first 
when month<2 then into t3 values(product_id,product_name,month)  
when month<3 then into t4 values(product_id,product_name,month)  
else into t5 values(product_id,product_name,month)  
select product_id,product_name,month from t2;

test=# select * from t3;
 product_id | product_name | month 
------------+--------------+-------
        111 | apple        |     1
        222 | orange       |     1
        333 | banana       |     1
(3 rows)

test=# select * from t4;
 product_id | product_name | month 
------------+--------------+-------
        111 | apple        |     2
        222 | orange       |     2
        333 | banana       |     2
(3 rows)

test=# select * from t5;
 product_id | product_name | month 
------------+--------------+-------
        111 | apple        |     3
        222 | orange       |     3
        333 | banana       |     3
        111 | apple        |     4
        222 | orange       |     4
        333 | banana       |     4
(6 rows)

四、总结

insert first 当数据满足第一when 条件的时候,不会作用在下面的when条件了,简而言之:当一条数据满足条件后,即使满足后面的条件也不会插入数据库。如果数据满足所有的when条件,insert all每个表都会插入该条数据,insert first 数据只会插入一个表。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值