LightDB支持INSERT ALL语法

转载自 https://blog.csdn.net/s_lisheng/article/details/130646539

Oracle中支持INSERT ALL语法,可进行多表插入与条件插入,对此,LightDB对该语法实现了兼容。

INSERT ALL官网文档

Oracle语法如下:
LightDB支持INSERT ALL语法_第1张图片
LightDB支持INSERT ALL语法_第2张图片
LightDB支持INSERT ALL语法_第3张图片

LightDB支持INSERT ALL语法_第4张图片
我们看一下代码示例:

create table multi_insert_t1(a int, b int);
create table multi_insert_t2(a int, b int);
create table multi_insert_t3(a int, b int);
create table multi_insert_t4(a int, b int);
create table multi_insert_t5(a int, b int);

-- test insert values
insert all into multi_insert_t1 values(1,1) select * from dual;
select * from multi_insert_t1 order by a, b;
insert all into multi_insert_t1 values(2,2) into multi_insert_t1 values(3,3) select * from dual;
select * from multi_insert_t1 order by a, b;
insert all into multi_insert_t1 values(4,4) into multi_insert_t2 values(1,1) select * from dual;
select * from multi_insert_t1 order by a, b;
select * from multi_insert_t2 order by a, b;
insert all into multi_insert_t1(a) values(5) select * from dual;
select * from multi_insert_t1 order by a, b;
insert all into multi_insert_t1(a) values(6) into multi_insert_t2(a) values(2) select * from dual;
select * from multi_insert_t1 order by a, b;
select * from multi_insert_t2 order by a, b;



-- test condition insert

insert all when a > 2 then into multi_insert_t2 when a < 2 then into multi_insert_t3 else into multi_insert_t4 select * from multi_insert_t1;
select * from multi_insert_t2 order by a, b;
select * from multi_insert_t3 order by a, b;
select * from multi_insert_t4 order by a, b;
truncate table multi_insert_t1;
insert all into multi_insert_t1 select * from multi_insert_t3 union select * from multi_insert_t4;
select * from multi_insert_t1 order by a, b;
insert all into multi_insert_t1 select * from multi_insert_t3 union all select * from multi_insert_t4;
select * from multi_insert_t1 order by a, b;


-- test insert from other table
truncate multi_insert_t3;
truncate multi_insert_t4;

insert into multi_insert_t3 values (1,1), (2,1), (3,1), (4,1);
insert all into multi_insert_t4(a,b) select a,b from multi_insert_t3;
select * from multi_insert_t4 order by a, b;
truncate multi_insert_t4;

insert all into multi_insert_t4(a,b) select b,a from multi_insert_t3;
select * from multi_insert_t4 order by a, b;
truncate multi_insert_t4;

insert all into multi_insert_t4 select b,a from multi_insert_t3;
select * from multi_insert_t4 order by a, b;
truncate multi_insert_t4;

insert all when a < 3 then into multi_insert_t4 when a >= 3 then into multi_insert_t5 select b,a from multi_insert_t3;
select * from multi_insert_t4 order by a, b;
select * from multi_insert_t5 order by a, b;
truncate multi_insert_t4;
truncate multi_insert_t5;

insert when a < 3 then into multi_insert_t4 when a >= 3 then into multi_insert_t5 select b,a from multi_insert_t3;
select * from multi_insert_t4 order by a, b;
select * from multi_insert_t5 order by a, b;
truncate multi_insert_t4;
truncate multi_insert_t5;

insert all when a < 3 then into multi_insert_t4(a, b) when a >= 3 then into multi_insert_t5(b, a) select b,a from multi_insert_t3;
select * from multi_insert_t4 order by a, b;
select * from multi_insert_t5 order by a, b;
truncate multi_insert_t4;
truncate multi_insert_t5;

insert all into multi_insert_t4(a,b) values(a, b)select b,a from multi_insert_t3;
select * from multi_insert_t4 order by a, b;
truncate multi_insert_t4;

insert all into multi_insert_t4(a,b) values(key1, a)select b as key1,a from multi_insert_t3;
select * from multi_insert_t4 order by a, b;
truncate multi_insert_t4;

insert all when a < 3 then into multi_insert_t4 values(a, 100) when a >= 3 then into multi_insert_t5 values(b, 1000) select b,a from multi_insert_t3;
select * from multi_insert_t4 order by a, b;
select * from multi_insert_t5 order by a, b;
truncate multi_insert_t4;
truncate multi_insert_t5;

insert all when a < 3 then into multi_insert_t3 values(a, 100) when a >= 3 then into multi_insert_t5 values(b, 1000) select * from(select b,a from multi_insert_t4);
insert all when a < 3 then into multi_insert_t3 values(a, 100) when a >= 3 then into multi_insert_t5 values(b, 1000) with cte as(select * from multi_insert_t4) select b,a from cte;
insert all when a < 3 then into multi_insert_t3 values(a, 100) when a >= 3 then into multi_insert_t5 values(b, 1000) select b,a from multi_insert_t2 where exists (select * from multi_insert_t4 where multi_insert_t4.a=multi_insert_t2.a);
insert all when a < 3 then into multi_insert_t3 values(a, 100) when a >= 3 then into multi_insert_t5 values(b, 1000) select b,a from multi_insert_t4 as source;

create schema test_insert_all;
create table test_insert_all.multi_insert_t3(a int, b int);
insert all when a < 3 then into test_insert_all.multi_insert_t3 values(a, 100) when a >= 3 then into multi_insert_t5 values(b, 1000) select b,a from multi_insert_t3 as source;
select * from test_insert_all.multi_insert_t3 order by a, b;
select * from multi_insert_t5 order by a, b;
truncate multi_insert_t5;
drop table test_insert_all.multi_insert_t3;
drop schema test_insert_all;

-- test when source table and target table is same
insert all when a < 3 then into multi_insert_t3 values(a, 100) when a >= 3 then into multi_insert_t5 values(b, 1000) select b,a from multi_insert_t3;
select * from multi_insert_t3 order by a, b;
insert all when a < 3 then into multi_insert_t3 values(a, 100) when a >= 3 then into multi_insert_t5 values(b, 1000) select * from(select b,a from multi_insert_t3);
select * from multi_insert_t3 order by a, b;
insert all when a < 3 then into multi_insert_t3 values(a, 100) when a >= 3 then into multi_insert_t5 values(b, 1000) with cte as(select * from multi_insert_t3) select b,a from cte;
select * from multi_insert_t3 order by a, b;
insert all when a < 3 then into multi_insert_t3 values(a, 100) when a >= 3 then into multi_insert_t5 values(b, 1000) select b,a from multi_insert_t2 where exists (select * from multi_insert_t3 where multi_insert_t2.a=multi_insert_t3.a);
select * from multi_insert_t3 order by a, b;
insert all when a < 3 then into multi_insert_t3 values(a, 100) when a >= 3 then into multi_insert_t5 values(b, 1000) select b,a from multi_insert_t3 as source;
select * from multi_insert_t3 order by a, b;

-- test plorasql
truncate multi_insert_t1;

begin
	insert into multi_insert_t1 values(1,1);
end;
/
select * from multi_insert_t1 order by a, b;

begin
	insert all into multi_insert_t1 values(2,2) select * from dual;
end;
/
select * from multi_insert_t1 order by a, b;

truncate multi_insert_t2;
begin
	insert all into multi_insert_t1 values(3,3) into multi_insert_t2 values(1,1) select * from dual;
end;
/
select * from multi_insert_t1 order by a, b;
select * from multi_insert_t2 order by a, b;
truncate multi_insert_t3;
begin
	insert all into multi_insert_t3 select * from multi_insert_t1;
end;
/
select * from multi_insert_t3 order by a, b;
truncate multi_insert_t4;
begin
	insert all when a > 2 then into multi_insert_t4 when a < 2 then into multi_insert_t1 else into multi_insert_t2 select * from multi_insert_t3;
end;
/
select * from multi_insert_t1 order by a, b;
select * from multi_insert_t2 order by a, b;
select * from multi_insert_t3 order by a, b;
select * from multi_insert_t4 order by a, b;

-- test complex query
create table multi_insert_t6(a int, b int, c int, d int);
insert all into multi_insert_t6 select * from multi_insert_t1 join multi_insert_t2 on multi_insert_t1.a = multi_insert_t2.a;
select * from multi_insert_t6 order by a, b;
insert all into multi_insert_t4 select * from multi_insert_t1 minus select * from multi_insert_t2;
select * from multi_insert_t4 order by a, b;
truncate multi_insert_t6;
insert all into multi_insert_t6 select * from multi_insert_t1 offset 1 row;
truncate multi_insert_t6;

insert all into multi_insert_t6 select * from multi_insert_t1 order by a, b offset 1 row;
select * from multi_insert_t6 order by a, b;
truncate multi_insert_t6;

insert all into multi_insert_t6 select * from multi_insert_t1 order by a, b desc offset 1 row;
select * from multi_insert_t6 order by a, b;
truncate multi_insert_t6;


-- error
insert all into multi_insert_t1 values(1,1) (select * from dual);


drop table multi_insert_t1;
drop table multi_insert_t2;
drop table multi_insert_t3;
drop table multi_insert_t4;
drop table multi_insert_t5;
drop table multi_insert_t6;

最后,需注意:Oracle多表插入语句受以下主要限制:

  • 只能对表执行多表插入,不能对视图或物化视图执行多表插入。
  • 不能在远程表中执行多表插入。
  • 一个表集合表达式不能在一个多表插入语句中使用。
  • 多表insert语句不支持计划稳定性。
  • 不能在多表insert语句的任何部分指定序列。多表插入被认为是一条SQL语句。

你可能感兴趣的:(lightdb,数据库,数据库,sql,oracle)